// ************************* 78 character ruler *****************************
// RFBB.c -- Implementation of FRIG RFBB 
//
// $Author: Liu Rongwu $
// $Header: /RFBB_TDDSW/FZHA_RFBB/Sources/RFBB/RFBB.c
// $History: 09/25/2010 create and ,modified by lh
//           12/25/2011 modified by Pedro Wang (En Qi) 
//
// (c) 2008 Nokia Siemens Networks.
// ************************* 78 character ruler *****************************

#include "rssmb.h"
#include <cvirte.h>
#include "RFBB.h"
//#include "ICD_PSU.h"

//#include <rs232.h>
#include <NIDAQmx.h>
#include <utility.h>

#include "PsuContorl.h"
#include "USB6501.h"
#include "rsnrpz.h"
#include "hpesg.h"
#include "PowerMeter.h"
#include "SetSignalGenerator.h"
#include "ElectronicLoad.h"
#include "agtn330x.h"

//#include <CalFileParser.h> 
#include "CVIMacros.h"
//#include "FMONLib.h" 
#include <formatio.h>
#include <ansi_c.h>
#include <userint.h>  

/////////////////////////////////////////////////////////////////////////////
// Globals
/////////////////////////////////////////////////////////////////////////////
CAObjHandle g_hEventLog = 0;
CAObjHandle g_hErrorLog = 0;
CAObjHandle g_hThreadPostUIMessage = 0;
CAObjHandle g_hCalFileParser =0;

unsigned short g_uiHwSwID =0;          // 
char g_szSwVersion[BUFFER_SIZE_256]={'\0'};        // 
char g_szHwVersion[16]={'\0'};         // 
char g_szProductCode[16]={'\0'};       // 
char g_szSerialNumber[16]={'\0'};      //

char g_szCommandToSend[BUFFER_SIZE_256] ={'\0'};
int  g_iCmonErrorCode = 0;
char g_szCmonErrorDescr[BUFFER_SIZE_256] ={'\0'}; 
char g_szCmonReadString[BUFFER_SIZE_512] ={'\0'};


//int g_hSignalGenerator;
int g_hSpecAnalyzer;
int g_HWConfigScript = 0;
int g_hProgressPanel= 0;
int g_hCmonAgent = 0;
int g_hFrmonAgent = 0;
int g_hShellCommand; 
int	g_iPort = 0;
int g_iShellCommandPort =0;
char g_szDUTIP[64] = {'\0'}; 


char* pszPSU0_DUT = 0;
char* pszPSU1_DUTFan = 0;



#define lenReadString 1024
#define lenStringToSend 1024
#define lenErrorDescr 512
#define EnMsg 1        // enables PopUp Error message box in DLL

static char filepath[] = "c:\\Projects\\Beispiel_02\\Beispiel_02_BLT.ini";		//Filepath to pass to DLL  
static int  ErrorCode;
static char ErrorDescr[BUFFER_SIZE_512];
static char ReadString[BUFFER_SIZE_1024];
static char StringToSend[BUFFER_SIZE_1024];

extern TaskHandle g_hTaskFiberOptic; // Port0,line4: DCSW2 - FiberOptic Engage/DisEngage (DO)


//add by Wendy xiao on Sept.20
double	   g_dFrequency_B = 0;
double	   g_dFrequency_M = 0;
double	   g_dFrequency_T = 0; // 3 vars

// -----------------------------------------------------------------------
// Function:   DllMain   
//             
//
int __stdcall DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
    switch (fdwReason)
    {
        case DLL_PROCESS_ATTACH:
            if (InitCVIRTE (hinstDLL, 0, 0) == 0)
            {
                return 0;	  /* out of memory */
            }
        
        break;
        
        case DLL_PROCESS_DETACH:
            
            CloseCVIRTE ();
        
        break;
    }

    return 1;
}

// -----------------------------------------------------------------------
// Function:   DllEntryPoint   
//             
//
int __stdcall DllEntryPoint (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
   /* Included for compatibility with Borland */

   return DllMain (hinstDLL, fdwReason, lpvReserved);
}


// -----------------------------------------------------------------------
// Function:   PostUIStatusMessage
// Purpose:    Post message to GUI
//             (Swift GUI requires this to be written twice??)
// 
int PostUIStatusMessage( char* pszMessage )
{
   TS_ThreadPostUIMessageEx (g_hThreadPostUIMessage, NULL,
                                       TS_UIMsg_ProgressText, 0.0,
                                       pszMessage, 0, VTRUE);
                                       
   TS_ThreadPostUIMessageEx (g_hThreadPostUIMessage, NULL,
                                       TS_UIMsg_ProgressText, 0.0,
                                       pszMessage, 0, VTRUE);
                                       
   return 0;
}

// -----------------------------------------------------------------------
// #####################add code block by Liu Rongwu######################
// Function:   GetActiveXRefFromTestStand
// Purpose:    Get global ActiveX references from TS 
//             Called in TestStand PreUUTLoop
// 
int __decldllexp __stdcall GetActiveXRefs(CAObjHandle seqContextCVI)
{
   ERRORINFO errorInfo;
   ErrMsg errMsg = {'\0'};
   int error = RET_OK;

   if(!g_hEventLog) {
      // Event log handle dispatch value from TS
      tsErrChk(TS_PropertyGetValIDispatch(seqContextCVI,&errorInfo, 
                        "StationGlobals.NSN.Station.Refs.EventLog",0,&g_hEventLog));
   }
  
   if(!g_hErrorLog) {
      // Error log handle dispatch value from TS
      tsErrChk(TS_PropertyGetValIDispatch(seqContextCVI,&errorInfo, 
                        "StationGlobals.NSN.Station.Refs.ErrorLog",0,&g_hErrorLog));
   }
  
   if(!g_hCalFileParser){ 
  	  tsErrChk(TS_PropertyGetValIDispatch (seqContextCVI, &errorInfo, 
         "StationGlobals.NSN.Station.Refs.CalFileParser", 0,&g_hCalFileParser));  
   }

   if(!g_hThreadPostUIMessage){
      tsErrChk(TS_SeqContextGetProperty (seqContextCVI, &errorInfo,
         TS_SeqContextThread, CAVT_OBJHANDLE,&g_hThreadPostUIMessage)); 
   } 

   
  // add by Wendy Xiao on Sept. 20
   /*
    sprintf(szPropertyName, "%s.PARAMETERS.FREQUENCY_B", TESTPLAN_BASE_PATH_COM );    
   tsErrChk(TS_PropertyGetValNumber(testData->seqContextCVI, &errorInfo, szPropertyName, 0, &g_dFrequency_B));
   
   sprintf(szPropertyName, "%s.PARAMETERS.FREQUENCY_M", TESTPLAN_BASE_PATH_COM );    
   tsErrChk(TS_PropertyGetValNumber(testData->seqContextCVI, &errorInfo, szPropertyName, 0, &g_dFrequency_M));
   
   sprintf(szPropertyName, "%s.PARAMETERS.FREQUENCY_T", TESTPLAN_BASE_PATH_COM );    
   tsErrChk(TS_PropertyGetValNumber(testData->seqContextCVI, &errorInfo, szPropertyName, 0, &g_dFrequency_T));
   // add above on Sept. 20th
   */
Error:  
   if(error < 0)
   {
      // Handle fatal error
      strcpy(errMsg,"ERROR: Cannot get EELog references!");
	  if (g_hErrorLog) EELog_INEventLogShow(g_hErrorLog,NULL);
   }

   return error;
}
// ----------------------------------------------------------------------
// Function:   DiscardActiveXRefObjHandles
// Purpose:    Release global ActiveX references
//             called in TestStand PostUUTLoop

void __decldllexp __stdcall DiscardActiveXRefs(void)
{
   // Discard EELog references
   CA_DISCARD_OBJ(g_hCalFileParser);
   CA_DISCARD_OBJ(g_hThreadPostUIMessage);
   CA_DISCARD_OBJ(g_hEventLog);  
   CA_DISCARD_OBJ(g_hErrorLog);    
  
}
#if 0
// --------------------------------------------------------------------------
// Function:   DisplayMessagePanel
// Purpose:    Set mouse cursor to hourglass type and display test progress  
//             in popup panel. 
// Parameters: char* pszName - message to be displayed in panel
// Return:     void
// 
void DisplayMessagePanel(char* pszMessage)
{
   SetMouseCursor (VAL_HOUR_GLASS_CURSOR);
   SetCtrlAttribute( g_hProgressPanel, PANEL_PROG_STRING_TEST, 
                     ATTR_CTRL_VAL, pszMessage );
   DisplayPanel( g_hProgressPanel );
   
   return;
}


// --------------------------------------------------------------------------
// Function:   HideMessagePanel
// Purpose:    Hides message panel displayed by DisplayMessagePanel and sets
//             mouse cursor back to default. 
//             Panel is discarded (released from memory) in CleanupFixture().
// Parameters: none
// Return:     void
// 
void HideMessagePanel()
{
   ProcessSystemEvents();
   HidePanel( g_hProgressPanel );
   SetMouseCursor (VAL_DEFAULT_CURSOR);
   
   return;
}
#endif
#if(ON_OFF != SIMU_VERS)
// --------------------------------------------------------------------------
// Function:   InitialiseInstruments
// Purpose:    Initialize instruments for RFBB including one PSU and one USBDIO
//              
// Parameters: tTestData *testData, tTestError *testError
// Return:     void 
void __decldllexp TX_TEST InitialiseInstruments(tTestData *testData, tTestError *testError)
{
   ERRORINFO errorInfo;    // required for tsErrChk
   int error = 0;          // required for tsErrChk
   ErrMsg errMsg = {'\0'}; // required for tsErrChk
// ErrMsg szLogMsg = {'\0'}; 
   unsigned char szDeviceAddress[255] = {'\0'};


// unsigned char szDeviceAddress[255] = {'\0'}; 
   unsigned char szDeviceAddressPsu0[BUFFER_SIZE_256 - 1] = {'\0'};  //N5767A
   unsigned int size = 0;
   
   char* pszValue = 0;
   char szPropertyName[BUFFER_SIZE_256 - 1] = {'\0'}; 

   // TESTEST
   double dTemp = 0.0;
   
   int iPowerMeterInitThreadID = 0;
   int iSignalGeneratorInitThreadID = 0;

// ViStatus viStatus = 0;

   if(g_hThreadPostUIMessage != 0)
   {   
      tsErrChk(TS_ThreadPostUIMessage( g_hThreadPostUIMessage, NULL,
                                       TS_UIMsg_ProgressText, 0.0,
                                       "Initializing Instruments...", VTRUE) );
   } 
   
   sprintf(szPropertyName, "%s.PARAMETERS.DUTIP", TESTPLAN_BASE_PATH_EXT );    
   tsErrChk(TS_PropertyGetValString(testData->seqContextCVI, &errorInfo, szPropertyName, 0, &pszValue));
   strcpy( g_szDUTIP, pszValue);
   #if 0
   sprintf(szPropertyName, "%s.PARAMETERS.Port", TESTPLAN_BASE_PATH_EXT );    
   tsErrChk(TS_PropertyGetValNumber(testData->seqContextCVI, &errorInfo, szPropertyName, 0, &dTemp));
   g_iPort = (int)dTemp;
   #endif
   sprintf(szPropertyName, "%s.PARAMETERS.FrPort", TESTPLAN_BASE_PATH_EXT );    
   tsErrChk(TS_PropertyGetValNumber(testData->seqContextCVI, &errorInfo, szPropertyName, 0, &dTemp));
   g_iPort = (int)dTemp;
   
   sprintf(szPropertyName, "%s.PARAMETERS.ShellCommandPort", TESTPLAN_BASE_PATH_EXT );    
   tsErrChk(TS_PropertyGetValNumber(testData->seqContextCVI, &errorInfo, szPropertyName, 0, &dTemp));
   g_iShellCommandPort = (int)dTemp;

  // ******************* PSU N5700 initialization  *************************

#ifndef NO_PSU
   InitPSU();
#endif
   
   // *********************** PowerMeter initialization *************************    
   ErrChk(EELog_INEventLogLogEvent (g_hEventLog, NULL, "Init PowerMeter..."));

   ErrChk(InitPowerMeter());
   Delay(0.5);
   
   // *********************** PowerMeter initialization *************************    
   ErrChk(EELog_INEventLogLogEvent (g_hEventLog, NULL, "PowerMeter initialized...")); 

   // *********************** USBDIO initialization *************************    
//   ErrChk(EELog_INEventLogLogEvent (g_hEventLog, NULL, "Init USB DIO...")); 
   
//   InitDIO();	  //need pins-defination input from MS.Fan 
   
   ErrChk(EELog_INEventLogLogEvent (g_hEventLog, NULL, "USB DIO initialized..."));   

   // *********************** Agn3300a initialization *************************    
   ErrChk(EELog_INEventLogLogEvent (g_hEventLog, NULL, "Init Agn3300a...")); 
   
   ErrChk(InitAgn3300a());
   Delay(0.5);
   
   // *********************** Agn3300a initialization *************************    
   ErrChk(EELog_INEventLogLogEvent (g_hEventLog, NULL, "Agn3300a initialized...")); 

   // *********************** Signal Generator initialization ************************* 
   //ErrChk(InitSignalGenerator());
   //Delay(0.5);    
   rssmb_init ("GPIB::28", VI_TRUE, VI_TRUE, &g_hSignalGenerator);
   ErrChk(EELog_INEventLogLogEvent (g_hEventLog, NULL, "smb100a initialized..."));  
   
   
  
Error:  
   // FREE RESOURCES
   CA_FREE( pszValue );
   // Post message to GUI
   TS_ThreadPostUIMessage( g_hThreadPostUIMessage, NULL,
                           TS_UIMsg_ProgressText, 0.0, "", VTRUE );
   #if(ON_OFF == SIMU_VERS)
   error=0;            //temporary line for debug L.R.W           
   #endif
   if ( error < 0 )
   {  // Handle fatal error //
      testError->errorFlag = TRUE; 
      testError->errorCode = error;
      testData->replaceStringFuncPtr(&testError->errorMessage, errMsg);
   }
   
   return;    
}
																						

// -----------------------------------------------------------------------
// Function:   CleanupInstruments   
//             
//
void __decldllexp TX_TEST CleanupInstruments(tTestData *testData, 
                                                      tTestError *testError)
{
   int error = 0;          // required for tsErrChk
   ErrMsg errMsg = {'\0'}; // required for tsErrChk

   // ********************* ALL PSU CLOSE  ************************* 
#ifndef NO_PSU  
   EELog_INEventLogLogEvent (g_hEventLog, NULL, "Closing ICD_PSUs...");
   ErrChk( PSU_Close(g_vi_sInitHandleN5700_1));
   //ErrChk( PSU_Unload() );
#endif   
   // *********************** USBDIO CLOSE *************************    
   //CleanupDio(testData, testError);	  //Clearup DIO status  
   //ErrChk(EELog_INEventLogLogEvent (g_hEventLog, NULL, "USB DIO close Completed...")); 
    if (g_hPowerMeter != 0 ) 
    {
      EELog_INEventLogLogEvent (g_hEventLog, NULL, "Closing powermeter...");
      rsnrpz_close (g_hPowerMeter);
      g_hPowerMeter = 0;
    }


    if (g_hSignalGenerator != 0) 
    {
      EELog_INEventLogLogEvent (g_hEventLog, NULL, "Closing Signal generator...");
	  rssmb_close (g_hSignalGenerator); 
	  g_hSignalGenerator = 0;
    }
	  
	
    
    if (g_vi_sInitHandleAGN3300A != 0) 
    {
      EELog_INEventLogLogEvent (g_hEventLog, NULL, "Closing Eload AgtN330X...");
      ErrChk( AgtN330X_close( g_vi_sInitHandleAGN3300A ));
      g_vi_sInitHandleAGN3300A = 0;
    }
	
	
	
	
	
			

    
Error:
    // FREE RESOURCES
   if ( error < 0 )
   { // Handle fatal error //
      EELog_INEventLogShow(g_hEventLog,NULL);   
      testError->errorFlag = TRUE; 
      testError->errorCode = error;
      testData->replaceStringFuncPtr(&testError->errorMessage, errMsg);
   }
   
   return;
}

// -----------------------------------------------------------------------
// Function:   PowerUpDut
// Purpose:    DUT is powered up with voltage/current limit settings from
//             testplan for PSU0. Overcurrent protection state is checked. 
//             Current consumption is measured and logged as event.
//
void __decldllexp TX_TEST PowerUpDut(tTestData *testData,  tTestError *testError)
{
   int error = 0;          // required for tsErrChk
   ErrMsg errMsg = {'\0'}; // required for tsErrChk
   ERRORINFO errorInfo;    // required for tsErrChk
   
   char* pszPowerSupply  = 0;          // testplan
   char szPropertyName[255] = {'\0'};
   char szLogMsg[255] = {'\0'};
   double dVoltage = 24;               // testplan, Cerberus 24V!
   double dCurrent = 1;                // testplan
   double dSettlingTime = 1;           // testplan
   int iOverCurrent = 0;
   
   sprintf(szPropertyName, "%s.PARAMETERS.PSU0_VOLTAGE", TESTPLAN_BASE_PATH_EXT );    
   tsErrChk(TS_PropertyGetValNumber(testData->seqContextCVI, &errorInfo, szPropertyName, 0, &dVoltage));

   sprintf(szPropertyName, "%s.PARAMETERS.PSU0_CURRENT", TESTPLAN_BASE_PATH_EXT );    
   tsErrChk(TS_PropertyGetValNumber(testData->seqContextCVI, &errorInfo, szPropertyName, 0, &dCurrent));
   
   sprintf(szPropertyName, "%s.PARAMETERS.PSU0_DELAY", TESTPLAN_BASE_PATH_EXT );    
   tsErrChk(TS_PropertyGetValNumber(testData->seqContextCVI, &errorInfo, szPropertyName, 0, &dSettlingTime));
   
   sprintf(szPropertyName, "%s.PARAMETERS.PSU0_LOGICALNAME", TESTPLAN_BASE_PATH_EXT );    
   tsErrChk(TS_PropertyGetValString(testData->seqContextCVI, &errorInfo, szPropertyName, 0, &pszPowerSupply));
   
   sprintf( szLogMsg, "Power up DUT, %.2fV, current limit %.2fA ", dVoltage, dCurrent );
   ErrChk( EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg));

#ifndef NO_PSU

   ErrChk(PSU_ClearOutputProtection (g_vi_sInitHandleN5700_1));
   ErrChk(PSU_SetVoltage(g_vi_sInitHandleN5700_1, dVoltage));
   ErrChk(PSU_SetCurrent(g_vi_sInitHandleN5700_1, dCurrent));
   ErrChk(PSU_SetOutputState( g_vi_sInitHandleN5700_1, 1 ));
   Delay( dSettlingTime );
   
   PSU_GetCurrentProtectionStatus( g_vi_sInitHandleN5700_1, &iOverCurrent );
   if( iOverCurrent != 0 ) 
    {                                                               
        strcpy(errMsg,"Output State  error on N5767A, Bad PSU");
        EELog_INEventLogLogEvent (g_hEventLog, NULL,  errMsg);
        error = -999;
    }   
   ErrChk(PSU_MeasureCurrent( g_vi_sInitHandleN5700_1, &dCurrent ));

#endif

   sprintf( szLogMsg, "Dut powered on, current consumption %.2fA", dCurrent );
   EELog_INEventLogLogEvent( g_hEventLog, NULL, szLogMsg );
   
Error:
   // FREE RESOURCES
   CA_FREE( pszPowerSupply );

   if ( error < 0 )
   { // Handle fatal error //
      sprintf( szLogMsg, "# Error in PowerUpDut! error: %d", error);
      EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg );
      EELog_INEventLogShow(g_hEventLog,NULL);   
      testError->errorFlag = TRUE; 
      testError->errorCode = error;
      testData->replaceStringFuncPtr(&testError->errorMessage, errMsg);
   }
  
   return;  
}

// -----------------------------------------------------------------------
// Function:   PowerDownDut (Action)
// Purpose:    DUT is powered down, PSU0 
//                                                                                         
void __decldllexp TX_TEST PowerDownDut(tTestData *testData, tTestError *testError)
{
   int error = 0;          // required for tsErrChk
   ErrMsg errMsg = {'\0'}; // required for tsErrChk
   ERRORINFO errorInfo;
   int iOverCurrent = 0;
   char szPropertyName[255] = {'\0'};   
   char* pszPowerSupply  = 0;          // testplan
   char szLogMsg[512] = {'\0'};  
   
   ErrChk( EELog_INEventLogLogEvent (g_hEventLog, NULL, "Power down DUT..."));
   sprintf(szPropertyName, "%s.PARAMETERS.PSU0_LOGICALNAME", TESTPLAN_BASE_PATH_EXT );
   tsErrChk(TS_PropertyGetValString(testData->seqContextCVI, &errorInfo, szPropertyName, 0, &pszPowerSupply));

#ifndef NO_PSU
   // Check overcurrent   
   ErrChk( PSU_GetCurrentProtectionStatus( g_vi_sInitHandleN5700_1, &iOverCurrent ));
   if( iOverCurrent != 0) {
      EELog_INEventLogLogEvent (g_hEventLog, NULL, "Clear PSU over current protection state...");
      ErrChk( PSU_ClearOutputProtection( g_vi_sInitHandleN5700_1));
   }
   else {   
      // Output OFF
      ErrChk( PSU_SetOutputState( g_vi_sInitHandleN5700_1, 0 ));
   }

#endif   
   
Error:
   // FREE RESOURCES
   CA_FREE( pszPowerSupply );

   if ( error < 0 )
   { // Handle fatal error //
      sprintf( szLogMsg, "# Error in PowerDownDut! error: %d", error);
      EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg );
      EELog_INEventLogShow(g_hEventLog,NULL);   
      testError->errorFlag = TRUE; 
      testError->errorCode = error;
      testData->replaceStringFuncPtr(&testError->errorMessage, errMsg);
   }
  
   return;
}


/*// -----------------------------------------------------------------------
// Function:   FixtureOpen
// Purpose:    Check if the fixture status is ready 
//				

void __decldllexp TX_TEST IsFixtureReady(tTestData *testData, tTestError *testError)
{
   int error = 0;
   ErrMsg errMsg = {'\0'};

   int iLoop = 0;
   int iDataRead = 999;

	
	do
	{
		iLoop++;

		iDataRead = ReadFixtureReady(); // port1/lin0(DI) - 1:FIXTURE_READY; 0:Not Ready
		if( (iDataRead != 1) && (iLoop <= 4) )
			ConfirmPopup ("Operator Instruction", "Fixture is NOT engaged!");
 		else
 			break;
		
 	 } while ( (iDataRead != 1) || (iLoop > 4));


    // FREE RESOURCES

    // If an error occurred, set the error flag to cause a run-time error in TestStand.
   if (error < 0)
   {
      testError->errorFlag = TRUE;
      testError->errorCode = error;
      testData->replaceStringFuncPtr(&testError->errorMessage, errMsg);
   }
    
   return;    
}*/

// ======================================================================================
// Function:   CmonAgent_Action
// Purpose:    
// Parameters: 

int CmonAgent_Action(char* pszCommand, int iTimeout)
{


    int iByte = 0;
    int	iPos = -1;
    int iTotalBytesRead = 0;
    int iResult = 0;
    char szBuffer[1024] = {'\0'};
    char szTemp[512] = {'\0'};

    double dTimeElapsed = 0.0;
    double dStartTime = 0.0;

    ErrMsg szLogMsg;


    if(iTimeout < 0)
    {
    	iTimeout = 5;	//second
    }

    InetTelnetWrite (g_hCmonAgent, pszCommand, "\r\n", -1, &iByte, T25000MS);
    EELog_INEventLogLogEvent (g_hEventLog, NULL, pszCommand); 

    Delay(0.1);

    dStartTime = Timer(); 

    while (1)
    {
    	InetTelnetRead (g_hCmonAgent, szTemp, sizeof(szTemp)-1, &iByte, 100);
    	strcat(szBuffer, szTemp);
    	iPos = FindPattern (szBuffer, 0, -1, "ret", 0, 0);
        if (iByte == 0 && iTotalBytesRead > 0 && iPos >= 0)
            break;
        iTotalBytesRead += iByte;
    	
        dTimeElapsed = Timer() - dStartTime;
        if( dTimeElapsed > iTimeout) 
        {
    		   sprintf(szLogMsg,"COMN Agent Timeout...");
    		   iResult = -1;
    		   goto Error;
        }	
    }
    
    EELog_INEventLogLogEvent (g_hEventLog, NULL, szBuffer); 

Error:	

    if(iResult <0 )
    {
         EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
    }

    return iResult;


}

// ======================================================================================
// Function:   CmonAgent_PassFail
// Purpose:    
// Parameters:
int CmonAgent_PassFail(char* pszCommand, int iTimeout, int* PassFail)
{


    int iByte = 0;
    int	iPos = -1;
    int iTotalBytesRead = 0;
    int iResult = 0;
    char szBuffer[BUF_SIZE_4K] = {'\0'};
    char szTemp[512] = {'\0'};

    double dTimeElapsed = 0.0;
    double dStartTime = 0.0;

    ErrMsg szLogMsg;


    if(iTimeout < 0)
    {
    	iTimeout = 10;	//second
    }

    InetTelnetWrite (g_hCmonAgent, pszCommand, "\r\n", -1, &iByte, T25000MS);
    EELog_INEventLogLogEvent (g_hEventLog, NULL, pszCommand); 

    Delay(0.1);

    dStartTime = Timer(); 

    while (1)
    {
    	InetTelnetRead (g_hCmonAgent, szTemp, sizeof(szTemp)-1, &iByte, 100);
    	strcat(szBuffer, szTemp);
    	iPos = FindPattern (szBuffer, 0, -1, "ret", 0, 0);
        if (iByte == 0 && iTotalBytesRead > 0 && iPos >= 0)
            break;
        iTotalBytesRead += iByte;
    	
       dTimeElapsed = Timer() - dStartTime;
       if( dTimeElapsed > iTimeout ) 
       {
    	   sprintf(szLogMsg,"CMON Agent Timeout...");
    	   iResult = -1;
    	   goto Error;
       }
    	
    	
    }
    EELog_INEventLogLogEvent (g_hEventLog, NULL, szBuffer);



    iPos = FindPattern(szBuffer ,0,-1,"error",0,0); 
    if(iPos >= 0) 
    { 
       sprintf( szLogMsg,"%s","CMON Agent Error Occured.");	   
       iResult = -1;
       goto Error;
    } 

    iPos = FindPattern (szBuffer, 0, -1, "FAIL", 0, 0);
    if(iPos >= 0)
    {
       sprintf(szLogMsg,"%s", "CMON Agent Executed Result Fail.");
       *PassFail = 0;
       iResult = -1;
       
    } 
    else
    {

       sprintf(szLogMsg,"%s", "Command Executed Sucessfully.");
       EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
       *PassFail = 1;
       iResult = 0;

    }



Error:	

    if(iResult <0 )
    {
         EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
        *PassFail = -999;
    }

    return iResult;


}
// ======================================================================================
// Function:   CmonAgent_Numeric
// Purpose:    
// Parameters: 

int CmonAgent_Numeric(char* pszCommand, int iTimeout, char* pszPattern, double* dMeasurement)
{


    int iByte = 0;
    int	iPos = -1;
    int i = 0;
    int iTotalBytesRead = 0;
    int iResult = 0;
    char szBuffer[1024] = {'\0'};
    char szTemp[512] = {'\0'};

    char* ptr = 0;  

    double dTimeElapsed = 0.0;
    double dStartTime = 0.0;

    ErrMsg szLogMsg;


    if(iTimeout < 0)
    {
    	iTimeout = 10;	//second
    }

    InetTelnetWrite (g_hCmonAgent, pszCommand, "\r\n", -1, &iByte, T25000MS);
    EELog_INEventLogLogEvent (g_hEventLog, NULL, pszCommand); 

    Delay(0.1);

    dStartTime = Timer(); 

    while (1)
    {
    	InetTelnetRead (g_hCmonAgent, szTemp, sizeof(szTemp)-1, &iByte, 100);
    	strcat(szBuffer, szTemp);
    	iPos = FindPattern (szBuffer, 0, -1, "ret", 0, 0);
        if (iByte == 0 && iTotalBytesRead > 0 && iPos >= 0)
            break;
        iTotalBytesRead += iByte;
    	
       dTimeElapsed = Timer() - dStartTime;
       if( dTimeElapsed > iTimeout ) 
       {
    	   sprintf(szLogMsg,"CMON Agent Timeout...");
    	   iResult = -1;
    	   goto Error;
       }
    	
    	
    }
    EELog_INEventLogLogEvent (g_hEventLog, NULL, szBuffer);



    iPos = FindPattern(szBuffer ,0,-1,"error",0,0); 
    if(iPos >= 0) 
    { 
       sprintf( szLogMsg,"%s","CMON Agent Error Occured.");	   
       iResult = -1;
       goto Error;
    } 

    iPos = FindPattern (szBuffer, 0, -1, "FAIL", 0, 0);
    if(iPos>=0)
    {
       sprintf(szLogMsg,"%s", "CMON Agent Executed Result Fail.");
       *dMeasurement = -99;
       iResult = -1;
       
    } 
    else
    {
       
    	ptr = strstr(szBuffer, pszPattern);
    	if(NULL == ptr)
        {   
    		return  -1;
        }
        
    	ptr = ptr + strlen(pszPattern);

    	while (*ptr != '\0' && ((*ptr >= 48 && *ptr <= 57) || *ptr == 45 || *ptr == 46 || *ptr == 32))
    	{
    		szTemp[i] = *ptr;

    		++ptr;
    		++i;
            if(i > MAXGETBYTE)
            {
                break;
            }
    	}
    	szTemp[i] = '\0';
    	*dMeasurement = atof(szTemp);
       
       
       iResult = 0;

    }



Error:	

    if(iResult <0 )
    {
         EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
        *dMeasurement = -999;
    }

    return iResult;


}
// ======================================================================================
// Function:   CmonAgent_String
// Purpose:    
// Parameters: 

int CmonAgent_String(char* pszCommand, int iTimeout, char* pszPattern, int* PassFail)
{


    int iByte = 0;
    int	iPos = -1;
    int iTotalBytesRead = 0;
    int iResult = 0;
    char szBuffer[BUF_SIZE_32K+1] = {'\0'};
    char szTemp[BUF_SIZE_32K+1] = {'\0'};

    double dTimeElapsed = 0.0;
    double dStartTime = 0.0;

    ErrMsg szLogMsg;


    if(iTimeout < 0)
    {
    	iTimeout = 10;	//second
    }

    InetTelnetWrite (g_hCmonAgent, pszCommand, "\r\n", -1, &iByte, T25000MS);
    EELog_INEventLogLogEvent (g_hEventLog, NULL, pszCommand); 

    Delay(0.1);

    dStartTime = Timer(); 

    while (1)
    {
    	InetTelnetRead (g_hCmonAgent, szTemp, sizeof(szTemp)-1, &iByte, 100);
    	strcat(szBuffer, szTemp);
    	iPos = FindPattern (szBuffer, 0, -1, "ret", 0, 0);
        if (iByte == 0 && iTotalBytesRead > 0 && iPos >= 0)
            break;
        iTotalBytesRead += iByte;
    	
        dTimeElapsed = Timer() - dStartTime;
        if( dTimeElapsed > iTimeout ) 
        {
           sprintf(szLogMsg,"CMON Agent Timeout...");
           iResult = -1;
           goto Error;
        }	
    }
    
    EELog_INEventLogLogEvent (g_hEventLog, NULL, szBuffer);



    iPos = FindPattern(szBuffer ,0,-1,"error",0,0); 
    if(iPos >= 0) 
    { 
       sprintf( szLogMsg,"%s","CMON Agent Error Occured.");	   
       *PassFail = 0;
       iResult = 0;
       goto Error;
    } 

    iPos = FindPattern (szBuffer, 0, -1, "FAIL", 0, 0);
    if(iPos>=0)
    {
       sprintf(szLogMsg,"%s", "CMON Agent Executed Result Fail.");
       *PassFail = 0;
       iResult = 0;
       
    } 
    else
    {

       iPos = FindPattern (szBuffer, 0, -1, pszPattern, 0, 0);
       if(iPos>=0)
       {
    	   sprintf(szLogMsg,"%s", "Command Executed Sucessfully.");
    	   EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
    	   
    	   *PassFail = 1;
    	   iResult = 0;
       }
       else
       {
    	   sprintf(szLogMsg,"%s", "Command Executed Sucessfully but .");
    	   EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
    	   *PassFail = 0;
    	   iResult = 0;
       }
    }

Error:	

    if(iResult < 0)
    {
         EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
        *PassFail = -999;
    }

    return iResult; 
}
// ======================================================================================
// Function:   CmonAgent_String
// Purpose:    
// Parameters:

/*
int CmonAgentMultiNumeric(char* pszCommand, int iTimeout, _FIND_PATTERN_AND_NUMERIC* pszFindPattern, int iTotlePatternNum,int* PassFail)
{


    int iByte = 0;
    int	iPos = -1;
    int iTotalBytesRead = 0;
    int iPatternNum = 0;
    int iResult = 0;
    int i = 0;
    char szBuffer[BUF_SIZE_32K+1] = {'\0'};
    char szTemp[BUF_SIZE_32K+1] = {'\0'};
    char* ptr = 0;
    _FIND_PATTERN_AND_NUMERIC* ptrTempPattern = NULL;
    char lszCommandAck[BUFFER_SIZE_256] = {'\0'};
    double dTimeElapsed = 0.0;
    double dStartTime = 0.0;

    ErrMsg szLogMsg;

    if((iTotlePatternNum <= 0) || (pszFindPattern == NULL))
    {
        return -1;
    }
    
    if(iTimeout < 0)
    {
    	iTimeout = 10;	//second
    }

    //ACK 
    sprintf(lszCommandAck,"ret %s",pszCommand);

    InetTelnetWrite (g_hCmonAgent, pszCommand, "\r\n", -1, &iByte, T25000MS);
    EELog_INEventLogLogEvent (g_hEventLog, NULL, pszCommand); 

    Delay(0.1);

    dStartTime = Timer(); 

    while (1)
    {
    	InetTelnetRead (g_hCmonAgent, szTemp, sizeof(szTemp)-1, &iByte, 100);
    	strcat(szBuffer, szTemp);
    	iPos = FindPattern (szBuffer, 0, -1, lszCommandAck, 0, 0);
        if (iByte == 0 && iTotalBytesRead > 0 && iPos >= 0)
            break;
        iTotalBytesRead += iByte;
    	
        dTimeElapsed = Timer() - dStartTime;
        if( dTimeElapsed > iTimeout ) 
        {
           sprintf(szLogMsg,"CMON Agent Timeout...");
           iResult = -1;
           goto Error;
        }	
    }
    
    EELog_INEventLogLogEvent (g_hEventLog, NULL, szBuffer);
    
    *PassFail = PASS;

    for(iPatternNum = 0;iPatternNum < iTotlePatternNum;iPatternNum++)
    {
        ptrTempPattern = (_FIND_PATTERN_AND_NUMERIC*)(pszFindPattern + iPatternNum);
        iPos = FindPattern(szBuffer ,0,-1,ptrTempPattern->cPatternString,0,0); 
        if(iPos < 0) 
        { 
           sprintf( szLogMsg,"Cant find the pattern :%s string and numeric.",ptrTempPattern->cPatternString);	
           EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
           *PassFail = 0;
           iResult = -1;
           
           goto Error;
        }
        else
        {
        	ptr = strstr(szBuffer, ptrTempPattern->cPatternString);
        	if(NULL == ptr)
        	{
        	    return  -1;
            }

        	ptr = ptr + strlen(ptrTempPattern->cPatternString);
            
            i = 0;
        	while (*ptr != '\0' && ((*ptr >= 48 && *ptr <= 57) || *ptr == 45 || *ptr == 46 || *ptr == 32))
        	{
        		szTemp[i] = *ptr;

        		++ptr;
        		++i;
                if(i < MAXGETBYTE)
                {
                    break;
                }
        	}
            
        	szTemp[i] = '\0';
            
        	ptrTempPattern->cFindNumeric = atof(szTemp);
           
           
           iResult = 0;            
        }
    }
 

Error:	

    if(iResult <0)
    {
         EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
        *PassFail = -999;
    }

    return iResult; 
}
*/

int CmonAgentMultiNumeric(char* pszCommand, int iTimeout, _FIND_PATTERN_AND_NUMERIC* pszFindPattern, int iTotlePatternNum)
{


    int iByte = 0;
    int	iPos = -1;
    int iTotalBytesRead = 0;
    int iPatternNum = 0;
    int iResult = 0;
    int i = 0;
    char szBuffer[BUF_SIZE_32K+1] = {'\0'};
    char szTemp[BUF_SIZE_32K+1] = {'\0'};
    char* ptr = 0;
    _FIND_PATTERN_AND_NUMERIC* ptrTempPattern = NULL;
    char lszCommandAck[BUFFER_SIZE_256] = {'\0'};
    double dTimeElapsed = 0.0;
    double dStartTime = 0.0;

    ErrMsg szLogMsg;

    if((iTotlePatternNum <= 0) || (pszFindPattern == NULL))
    {
        return -1;
    }
    
    if(iTimeout < 0)
    {
    	iTimeout = 10;	//second
    }

    //ACK 
    sprintf(lszCommandAck,"ret %s",pszCommand);

    InetTelnetWrite (g_hCmonAgent, pszCommand, "\r\n", -1, &iByte, T25000MS);
    EELog_INEventLogLogEvent (g_hEventLog, NULL, pszCommand); 

    Delay(0.1);

    dStartTime = Timer(); 

    while (1)
    {
    	InetTelnetRead (g_hCmonAgent, szTemp, sizeof(szTemp)-1, &iByte, 100);
    	strcat(szBuffer, szTemp);
    	iPos = FindPattern (szBuffer, 0, -1, lszCommandAck, 0, 0);
        if (iByte == 0 && iTotalBytesRead > 0 && iPos >= 0)
            break;
        iTotalBytesRead += iByte;
    	
        dTimeElapsed = Timer() - dStartTime;
        if( dTimeElapsed > iTimeout ) 
        {
           sprintf(szLogMsg,"CMON Agent Timeout...");
           iResult = -1;
           goto Error;
        }	
    }
    
    EELog_INEventLogLogEvent (g_hEventLog, NULL, szBuffer);
    

    for(iPatternNum = 0;iPatternNum < iTotlePatternNum;iPatternNum++)
    {
        ptrTempPattern = (_FIND_PATTERN_AND_NUMERIC*)(pszFindPattern + iPatternNum);
        iPos = FindPattern(szBuffer ,0,-1,ptrTempPattern->cPatternString,0,0); 
        if(iPos < 0) 
        { 
           sprintf( szLogMsg,"Cant find the pattern :%s string and numeric.",ptrTempPattern->cPatternString);	
           EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
           iResult = -1;
           
           goto Error;
        }
        else
        {
        	ptr = strstr(szBuffer, ptrTempPattern->cPatternString);
        	if(NULL == ptr)
        	{
        	    return  -1;
            }

        	ptr = ptr + strlen(ptrTempPattern->cPatternString);
            
            i = 0;
        	while (*ptr != '\0' && ((*ptr >= 48 && *ptr <= 57) || *ptr == 45 || *ptr == 46 || *ptr == 32))
        	{
        		szTemp[i] = *ptr;

        		++ptr;
        		++i;
                if(i > MAXGETBYTE)
                {
                    break;
                }
        	}
            
        	szTemp[i] = '\0';
            
        	ptrTempPattern->cFindNumeric = atof(szTemp);
                      
            iResult = 0;            
        }
    }
 

Error:	

    if(iResult <0)
    {
         EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
		 ptrTempPattern->cFindNumeric = -999;
    }

    return iResult; 
}

int CmonAgentMultiString(char* pszCommand, int iTimeout, _FIND_PATTERN_GET_SRING* pszFindPattern, int iTotlePatternNum,int* PassFail)
{


    int iByte = 0;
    int	iPos = -1;
    int iTotalBytesRead = 0;
    int iPatternNum = 0;
    int iGetStringLength = 0;
    int iResult = 0;
    
    char szBuffer[BUF_SIZE_32K+1] = {'\0'};
    char szTemp[BUF_SIZE_32K+1] = {'\0'};
    char* ptr = NULL;
    char szTempGetString[BUFFER_SIZE_64] = {'\0'};
    _FIND_PATTERN_GET_SRING* ptrTempPattern = NULL;
    char lszCommandAck[BUFFER_SIZE_256] = {'\0'}; 
    double dTimeElapsed = 0.0;
    double dStartTime = 0.0;

    ErrMsg szLogMsg;

    if((iTotlePatternNum <= 0) || (pszFindPattern == NULL))
    {
        return -1;
    }
    
    if(iTimeout < 0)
    {
    	iTimeout = 10;	//second
    }
    
    //ACK 
    sprintf(lszCommandAck,"ret %s",pszCommand);
        
    InetTelnetWrite (g_hCmonAgent, pszCommand, "\r\n", -1, &iByte, T25000MS);
    EELog_INEventLogLogEvent (g_hEventLog, NULL, pszCommand); 

    Delay(0.1);

    dStartTime = Timer(); 

    while (1)
    {
    	InetTelnetRead (g_hCmonAgent, szTemp, sizeof(szTemp)-1, &iByte, 100);
    	strcat(szBuffer, szTemp);
    	iPos = FindPattern (szBuffer, 0, -1, lszCommandAck, 0, 0);//lszCmonAck = ret + command
        if (iByte == 0 && iTotalBytesRead > 0 && iPos >= 0)
            break;
        iTotalBytesRead += iByte;
    	
        dTimeElapsed = Timer() - dStartTime;
        if( dTimeElapsed > iTimeout ) 
        {
           sprintf(szLogMsg,"CMON Agent Timeout...");
           iResult = -1;
           goto Error;
        }	
    }
    
    EELog_INEventLogLogEvent (g_hEventLog, NULL, szBuffer);
    
    *PassFail = PASS;

    for(iPatternNum = 0;iPatternNum < iTotlePatternNum;iPatternNum++)
    {
        ptrTempPattern = (_FIND_PATTERN_GET_SRING*)(pszFindPattern + iPatternNum);
        iPos = FindPattern(szBuffer ,0,-1,ptrTempPattern->cFindPattern,0,0); 
        if(iPos < 0) 
        { 
           sprintf( szLogMsg,"Cant find the pattern :%s string.",ptrTempPattern->cFindPattern);	
           EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
           *PassFail = 0;
           iResult = -1;
           
           goto Error;
        }
        else
        {
        	ptr = strstr(szBuffer, ptrTempPattern->cFindPattern);
        	if(NULL == ptr)
        	{
        	    return  -1;
            }
            
            //Get the String address
        	ptr = ptr + strlen(ptrTempPattern->cFindPattern) + ptrTempPattern->iOffsetFromPattern;

            iGetStringLength = strlen(ptrTempPattern->cGetString);
            if(0 >= iGetStringLength)
            {
                return  -1;
            }
            
            strncpy(szTempGetString,ptr,iGetStringLength);
            szTempGetString[iGetStringLength + 1] = '\0';
            
            if(0 != strcmp(szTempGetString,ptrTempPattern->cGetString))
            {
                sprintf( szLogMsg,"Cant find the pattern :%s string.",ptrTempPattern->cGetString);	
                EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);            
                *PassFail = 0;
                iResult = -1;

                goto Error;
            }
            iResult = 0;            
        }
    }
 

Error:	

    if(iResult <0)
    {
         EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
        *PassFail = -999;
    }

    return iResult; 
}

// ======================================================================================
// Function:   CmonAgentWriteReadUntil
// Purpose:    
// Parameters: 

int CmonAgentWriteReadUntil( char* pszCommand,char* pszSearchString,char*  pszReturnString, long iTimeOut,int iPrintLog  )
{
    int error = -1;

    //  char szOseScriptLog[10240] = {'\0'};
    int  iBytesWritten  = 0;
    int  iBytesRead = 0;
    int  iPos = -1;

    char szLogMsg[64000] = {'\0'}; 
    // char szTemp[10240] = {'\0'};

    if (iPrintLog == 1) {
      sprintf(szLogMsg,"%s CmonAgent:%s","->",pszCommand);
      EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
    }
    error = InetTelnetWrite (g_hCmonAgent, pszCommand, "\r\n", -1, &iBytesWritten, 5000); 
    if (error !=0) {
      goto Error;
    }
    strcpy(pszReturnString,""); 
    if (StringLength (pszSearchString) > 0) {

     //Not ready RiM
      //gives non-fatal runtime error if searchString does not exist
      //error = InetTelnetReadUntil (g_hOseShell,kszReturnString ,BUF_SIZE_32K , pszSearchString, iTimeOut);
      
      while (iPos < 0) {
         error = InetTelnetRead (g_hCmonAgent,pszReturnString ,BUF_SIZE_32K,&iBytesRead, 10);
         iPos = FindPattern (pszReturnString, 0, -1, pszSearchString, 1, 0);
         iTimeOut = iTimeOut - 10;
         if (iTimeOut <= 0)  {
             error = -1;
			 break;
         }
      }

    }
    else {
      error = InetTelnetRead (g_hCmonAgent,pszReturnString ,BUF_SIZE_32K,&iBytesRead, iTimeOut);
    }  
    //   iPos = FindPattern (pszReturnString, 0, -1, "\n", 1, 0);  

    //  CopyString (szTemp, 0, pszReturnString, iPos, -1);


Error:
	if (iPrintLog == 1 || error == -1) {
	  sprintf(szLogMsg,"%s TRX:%s","->",pszCommand);
	  EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
	  
	  sprintf(szLogMsg,"%s TRX:%s, Bytes: %d, Timeout:%d","<-",pszReturnString, iBytesRead,iTimeOut);
	  EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
	}

return error;   
}


#if 0
// ======================================================================================
// Function:   CmonAgent_Serial
// Purpose:    
// Parameters: 
int CmonAgent_Serial(char* pszCommand, int iTimeout, char* pszPattern, int* PassFail)
{
	
	
	int iByte = 0;
	int	iPos = -1;
	int iTotalBytesRead = 0;
	int iResult = 0;
	char szBuffer[2048] = {'\0'};
	char szTemp[BUF_SIZE_32K+1] = {'\0'};

    double dTimeElapsed = 0.0;
    double dStartTime = 0.0;
   
	ErrMsg szLogMsg;
	
	
	if(iTimeout < 0)
	{
		iTimeout = 10;	//second
	}

	InetTelnetWrite (g_hCmonAgent, pszCommand, "\r\n", -1, &iByte, 25000);
	Delay(2);
    EELog_INEventLogLogEvent (g_hEventLog, NULL, pszCommand); 
	EELog_INEventLogLogEvent (g_hEventLog,NULL,"###Open Serial port and sent HI");   
	Delay(0.1);
    iResult = OpenComConfig (1, "", 9600, 0, 8, 1, 512, 512); 
	if(iResult<0)
	    goto Error;
	iResult = 	ComWrt (1,"HI", 3);  
	Delay(1);
	if(iResult<0)
	    goto Error;
	
    dStartTime = Timer(); 
    while (1)
    {
		InetTelnetRead (g_hCmonAgent, szTemp, sizeof(szTemp)-1, &iByte, 100);
		strcat(szBuffer, szTemp);
		iPos = FindPattern (szBuffer, 0, -1, "ret", 0, 0);
        if (iByte == 0 && iTotalBytesRead > 0 && iPos >= 0)
            break;
        iTotalBytesRead += iByte;
		
        dTimeElapsed = Timer() - dStartTime;
        if( dTimeElapsed > iTimeout ) 
        {
		    sprintf(szLogMsg,"CMON Agent Timeout...");
		    iResult = -1;
		    goto Error;
        }		
    }
    EELog_INEventLogLogEvent (g_hEventLog, NULL, szBuffer);
	
    iPos = FindPattern(szBuffer ,0,-1,"error",0,0); 
    if(iPos >= 0) 
    { 
	    sprintf( szLogMsg,"%s","CMON Agent Error Occured.");	   
	    *PassFail = 0;
	    iResult = -1;
	    goto Error;
    } 
   
    iPos = FindPattern (szBuffer, 0, -1, "FAIL", 0, 0);
    if(iPos>=0)
    {
	    sprintf(szLogMsg,"%s", "CMON Agent Executed Result Fail.");
	    *PassFail = 0;
	    iResult = -1;
	} 
    else
    {
        iPos = FindPattern (szBuffer, 0, -1, pszPattern, 0, 0);
   	    if(iPos>=0)
	    {
		    sprintf(szLogMsg,"%s", "Command Executed Sucessfully.");
		    EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
		   
		    *PassFail = 1;
		    iResult = 0;
	    }
	    else
	    {
		    sprintf(szLogMsg,"%s", "Command Executed Sucessfully but .");
		    EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
		    *PassFail = 0;
		    iResult = -1;
   	    }
    }
	
Error:	
	
	if(iResult <0 )
	{
	     EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
	     *PassFail = -999;
	}
	
	return iResult; 
}


#endif
// -----------------------------------------------------------------------
// Function:   ShellCommand_Numeric  
// Purpose:               . 
//             
// Parameters: 
//
int ShellCommand_Numeric(char* pszCommand, int iTimeout, char* pszPattern, double* dMeasurement)
{


    int iByte = 0;
    int	iPos = -1;
    int i = 0;
    int iTotalBytesRead = 0;
    int iResult = 0;
    char szBuffer[2048] = {'\0'};
    char szTemp[512] = {'\0'};

    char* ptr = 0;  

    double dTimeElapsed = 0.0;
    double dStartTime = 0.0;

    ErrMsg szLogMsg;


    if(iTimeout < 0)
    {
    	iTimeout = 2;	//second
    }

    // Clear the welcome information
    InetTelnetRead (g_hShellCommand, szTemp, sizeof(szTemp)-1, &iByte, 100);
    iByte = 0;
    *szTemp = '\0';



    InetTelnetWrite (g_hShellCommand, pszCommand, "\r\n", -1, &iByte, T25000MS);
    EELog_INEventLogLogEvent (g_hEventLog, NULL, pszCommand); 

    Delay(0.1);

    dStartTime = Timer(); 

    while (1)
    {
    	InetTelnetRead (g_hShellCommand, szTemp, sizeof(szTemp)-1, &iByte, 100);
    	strcat(szBuffer, szTemp);
    	iPos = FindPattern (szBuffer, 0, -1, "$", 0, 0);
        if (iByte == 0 && iTotalBytesRead > 0 && iPos >= 0)
            break;
        iTotalBytesRead += iByte;
    	
       dTimeElapsed = Timer() - dStartTime;
       if( dTimeElapsed > iTimeout ) 
       {
    	   sprintf(szLogMsg,"ShellCommand Timeout...");
    	   iResult = -1;
    	   goto Error;
       }
    	
    	
    }
    EELog_INEventLogLogEvent (g_hEventLog, NULL, szBuffer);


    #if 0
    iPos = FindPattern(szBuffer ,0,-1,"error",0,0); 
    if(iPos >= 0) 
    { 
       sprintf( szLogMsg,"%s","ShellCommand  Error Occured.");	   
       iResult = -1;
       goto Error;
    } 
    #endif
    
    iPos = FindPattern (szBuffer, 0, -1, "FAIL", 0, 0);
    if(iPos >= 0)
    {
       sprintf(szLogMsg,"%s", "ShellCommand  Executed Result Fail.");
       *dMeasurement = -99;
       iResult = 0;
       
    } 
    else
    {
       
    	ptr = strstr(szBuffer, pszPattern);
    	if(NULL == ptr)
    		return  -1;

    	ptr = ptr + strlen(pszPattern);

    	while ((*ptr != '\0') && ((*ptr >=48 && *ptr <=57) || *ptr == 43 || *ptr == 45 || *ptr == 46 || *ptr == 32))
    	{
    		szTemp[i] = *ptr;

    		++ptr;
    		++i;
    	}
    	szTemp[i] = '\0';
    	*dMeasurement = atof(szTemp);
       
       
       iResult = 0;

    }

Error:	

    if(iResult <0 )
    {
         EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
        *dMeasurement = -999;
    }

return iResult;  

}

// -----------------------------------------------------------------------
// Function:   ShellCommand_Numeric  
// Purpose:               . 
//             
// Parameters: 
//
int ShellCommand_HexNumeric(char* pszCommand, int iTimeout, char* pszPattern, double* dMeasurement)
{


    int iByte = 0;
    int	iPos = -1;
    int i = 0;
    int iTotalBytesRead = 0;
    int iResult = 0;
    char szBuffer[1024] = {'\0'};
    char szTemp[512] = {'\0'};

    char* ptr = 0;  

    double dTimeElapsed = 0.0;
    double dStartTime = 0.0;

    ErrMsg szLogMsg;
    int iGetValue = 0;

    char szTempString[20] = {'\0'};
    double dGetValue = 0.0;
    
    if(iTimeout < 0)
    {
    	iTimeout = 2;	//second
    }

    // Clear the welcome information
    InetTelnetRead (g_hShellCommand, szTemp, sizeof(szTemp)-1, &iByte, 100);
    iByte = 0;
    *szTemp = '\0';



    InetTelnetWrite (g_hShellCommand, pszCommand, "\r\n", -1, &iByte, T25000MS);
    EELog_INEventLogLogEvent (g_hEventLog, NULL, pszCommand); 

    Delay(0.1);

    dStartTime = Timer(); 

    while (1)
    {
    	InetTelnetRead (g_hShellCommand, szTemp, sizeof(szTemp)-1, &iByte, 100);
    	strcat(szBuffer, szTemp);
    	iPos = FindPattern (szBuffer, 0, -1, "$", 0, 0);
        if (iByte == 0 && iTotalBytesRead > 0 && iPos >= 0)
            break;
        iTotalBytesRead += iByte;
    	
       dTimeElapsed = Timer() - dStartTime;
       if( dTimeElapsed > iTimeout ) 
       {
    	   sprintf(szLogMsg,"ShellCommand Timeout...");
    	   iResult = -1;
    	   goto Error;
       }
    	
    	
    }
    EELog_INEventLogLogEvent (g_hEventLog, NULL, szBuffer);


    #if 0
    iPos = FindPattern(szBuffer ,0,-1,"error",0,0); 
    if(iPos >= 0) 
    { 
       sprintf( szLogMsg,"%s","ShellCommand  Error Occured.");	   
       iResult = -1;
       goto Error;
    } 
    #endif
    
    iPos = FindPattern (szBuffer, 0, -1, "FAIL", 0, 0);
    if(iPos >= 0)
    {
       sprintf(szLogMsg,"%s", "ShellCommand  Executed Result Fail.");
       *dMeasurement = -99;
       iResult = 0;
       
    } 
    else
    {
        
    	ptr = strstr(szBuffer, pszPattern);
    	if(NULL == ptr)
    		return  -1;
        
    	ptr = ptr + strlen(pszPattern);
//    	while ((*ptr != '\0') && ((*ptr >=48 && *ptr <=57)||(*ptr >=97 && *ptr <=70)||(*ptr >=65 && *ptr <=70) || (*ptr ==120)))

    	while ((*ptr != '\0') &&(*ptr != 32))
    	{
    		szTemp[i] = *ptr;
            
    		++ptr;
    		++i;
    	}
    	szTemp[i] = '\0'; // 1 4 a          


        *dMeasurement = strtol(szTemp,NULL,16);	
    	
        //*dMeasurement = dGetValue;
       
       
       iResult = 0;

    }

Error:	

    if(iResult <0 )
    {
         EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
        *dMeasurement = -999;
    }

return iResult;  

}

// -----------------------------------------------------------------------
// Function:   ShellCommand_Numeric  
// Purpose:               . 
//             
// Parameters: 
//
int Shmon_IntegerNumeric(char* pszCommand, int iTimeout, char* pszPattern, long int* lMeasurement)
{


    int iByte = 0;
    int	iPos = -1;
    int i = 0;
    int iTotalBytesRead = 0;
    int iResult = 0;
    char szBuffer[1024] = {'\0'};
    char szTemp[512] = {'\0'};

    char* ptr = 0;  

    double dTimeElapsed = 0.0;
    double dStartTime = 0.0;

    ErrMsg szLogMsg;
    int iGetValue = 0;

    char szTempString[20] = {'\0'};
    double dGetValue = 0.0;
    
    if(iTimeout < 0)
    {
    	iTimeout = 2;	//second
    }

    // Clear the welcome information
    InetTelnetRead (g_hShellCommand, szTemp, sizeof(szTemp)-1, &iByte, 100);
    iByte = 0;
    *szTemp = '\0';



    InetTelnetWrite (g_hShellCommand, pszCommand, "\r\n", -1, &iByte, T25000MS);
    EELog_INEventLogLogEvent (g_hEventLog, NULL, pszCommand); 

    Delay(0.1);

    dStartTime = Timer(); 

    while (1)
    {
    	InetTelnetRead (g_hShellCommand, szTemp, sizeof(szTemp)-1, &iByte, 100);
    	strcat(szBuffer, szTemp);
    	iPos = FindPattern (szBuffer, 0, -1, "$", 0, 0);
        if (iByte == 0 && iTotalBytesRead > 0 && iPos >= 0)
            break;
        iTotalBytesRead += iByte;
    	
       dTimeElapsed = Timer() - dStartTime;
       if( dTimeElapsed > iTimeout ) 
       {
    	   sprintf(szLogMsg,"ShellCommand Timeout...");
    	   iResult = -1;
    	   goto Error;
       }
    	
    	
    }
    EELog_INEventLogLogEvent (g_hEventLog, NULL, szBuffer);


    #if 0
    iPos = FindPattern(szBuffer ,0,-1,"error",0,0); 
    if(iPos >= 0) 
    { 
       sprintf( szLogMsg,"%s","ShellCommand  Error Occured.");	   
       iResult = -1;
       goto Error;
    } 
    #endif
    
    iPos = FindPattern (szBuffer, 0, -1, "FAIL", 0, 0);
    if(iPos >= 0)
    {
       sprintf(szLogMsg,"%s", "ShellCommand  Executed Result Fail.");
       *lMeasurement = -99;
       iResult = 0;
       
    } 
    else
    {
        
    	ptr = strstr(szBuffer, pszPattern);
    	if(NULL == ptr)
    		return  -1;
        
    	ptr = ptr + strlen(pszPattern);
//    	while ((*ptr != '\0') && ((*ptr >=48 && *ptr <=57)||(*ptr >=97 && *ptr <=70)||(*ptr >=65 && *ptr <=70) || (*ptr ==120)))

    	while ((*ptr != '\0') &&(*ptr != 32))
    	{
    		szTemp[i] = *ptr;
            
    		++ptr;
    		++i;
    	}
    	szTemp[i] = '\0'; // 1 4 a          


        *lMeasurement = strtol(szTemp,NULL,16);	
    	
        //*dMeasurement = dGetValue;
       
       
       iResult = 0;

    }

Error:	

    if(iResult <0 )
    {
         EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
        *lMeasurement = -999;
    }

return iResult;  

}

// -----------------------------------------------------------------------
// Function:  ShellCommand_Action  
// Purpose:               . 
//             
// Parameters: 
//


int ShellCommand_Action(char* pszCommand, int iTimeout)
{


    int iByte = 0;
    int	iPos = -1;
    int iTotalBytesRead = 0;
    int iResult = 0;
    char szBuffer[1024] = {'\0'};
    char szTemp[512] = {'\0'};

    double dTimeElapsed = 0.0;
    double dStartTime = 0.0;

    ErrMsg szLogMsg;


    if(iTimeout < 0)
    {
    	iTimeout = 20;	//second
    }

    // Clear the welcome information
    InetTelnetRead (g_hShellCommand, szTemp, sizeof(szTemp)-1, &iByte, 100);
    iByte = 0;
    *szTemp = '\0';


    InetTelnetWrite (g_hShellCommand, pszCommand, "\r\n", -1, &iByte, T25000MS);
    EELog_INEventLogLogEvent (g_hEventLog, NULL, pszCommand); 

    Delay(0.1);

    dStartTime = Timer(); 

    while (1)
    {
    	InetTelnetRead (g_hShellCommand, szTemp, sizeof(szTemp)-1, &iByte, 100);
    	strcat(szBuffer, szTemp);
    	iPos = FindPattern (szBuffer, 0, -1, "$", 0, 0);
        if (iByte == 0 && iTotalBytesRead > 0 && iPos >= 0)
            break;
        iTotalBytesRead += iByte;
    	
        dTimeElapsed = Timer() - dStartTime;
        if( dTimeElapsed > iTimeout) 
        {
    		   sprintf(szLogMsg,"ShellCommand Timeout...");
    		   iResult = -1;
    		   goto Error;
        }
    	
    }
    EELog_INEventLogLogEvent (g_hEventLog, NULL, szBuffer); 

Error:	

    if(iResult <0 )
    {
         EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
    }

    return iResult;


}
// -----------------------------------------------------------------------
// Function:   ShellCommand_Hex  
// Purpose:               . 
//             
// Parameters: 
int ShellCommand_Hex(char* pszCommand, int iTimeout, char* pszPattern, char* pszHex)
{   
    int iByte = 0;
    int	iPos = -1;
    int i = 0;
    int iTotalBytesRead = 0;
    int iResult = 0;
    char szBuffer[1024] = {'\0'};
    char szTemp[512] = {'\0'};

    char* ptr = 0;  

    double dTimeElapsed = 0.0;
    double dStartTime = 0.0;

    ErrMsg szLogMsg;


    if(iTimeout < 0)
    {
    	iTimeout = 20;	//second
    }

    // Clear the welcome information
    InetTelnetRead (g_hShellCommand, szTemp, sizeof(szTemp)-1, &iByte, 100);
    iByte = 0;
    *szTemp = '\0';



    InetTelnetWrite (g_hShellCommand, pszCommand, "\r\n", -1, &iByte, T25000MS);
    EELog_INEventLogLogEvent (g_hEventLog, NULL, pszCommand); 

    Delay(0.1);

    dStartTime = Timer(); 

    while (1)
    {
    	InetTelnetRead (g_hShellCommand, szTemp, sizeof(szTemp)-1, &iByte, 100);
    	strcat(szBuffer, szTemp);
    	iPos = FindPattern (szBuffer, 0, -1, "$", 0, 0);
        if (iByte == 0 && iTotalBytesRead > 0 && iPos >= 0)
            break;
        iTotalBytesRead += iByte;
    	
       dTimeElapsed = Timer() - dStartTime;
       if( dTimeElapsed > iTimeout ) 
       {
    	   sprintf(szLogMsg,"ShellCommand Timeout...");
    	   iResult = -1;
    	   goto Error;
       }
    	
    	
    }
    EELog_INEventLogLogEvent (g_hEventLog, NULL, szBuffer);



    iPos = FindPattern(szBuffer ,0,-1,"error",0,0); 
    if(iPos >= 0) 
    { 
       sprintf( szLogMsg,"%s","ShellCommand  Error Occured.");	   
       iResult = -1;
       goto Error;
    } 

    iPos = FindPattern (szBuffer, 0, -1, "FAIL", 0, 0);
    if(iPos>=0)
    {
       sprintf(szLogMsg,"%s", "ShellCommand  Executed Result Fail.");
       *pszHex = '\0';
       iResult = -1;
       
    } 
    else
    {
       
    	ptr = strstr(szBuffer, pszPattern);
    	if(NULL == ptr)
    		return  -1;

    	ptr = ptr + strlen(pszPattern);

    	while (*ptr != '\0' && ((*ptr >=48 && *ptr <=57) || 
    		                    (*ptr >=65 && *ptr <=70) || 
    							(*ptr >=97 && *ptr <=102)|| 
    							 *ptr == 45 || *ptr == 46 || *ptr == 32))
    	{
    		szTemp[i] = *ptr;

    		++ptr;
    		++i;
    	}
    	szTemp[i] = '\0';
       
        strcpy(pszHex, szTemp);
        iResult = 0;

    }



Error:	

    if(iResult <0 )
    {
         EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
         *pszHex = '\0';
    }

    return iResult;


}
// -----------------------------------------------------------------------
// Function:   ShellCommand_String  
// Purpose:               . 
//             
// Parameters: 
int ShellCommand_StringSpecial(char* pszCommand, int iTimeout, char* pszPattern, int* PassFail)
{


    int iByte = 0;
    int	iPos = -1;
    int iTotalBytesRead = 0;
    int iResult = 0;
    char szBuffer[1024] = {'\0'};
    char szTemp[512] = {'\0'};

    double dTimeElapsed = 0.0;
    double dStartTime = 0.0;

    ErrMsg szLogMsg;


    if(iTimeout < 0)
    {
    	iTimeout = 20;	//second
    }

    InetTelnetWrite (g_hShellCommand, pszCommand, "\r\n", -1, &iByte, T25000MS);
    EELog_INEventLogLogEvent (g_hEventLog, NULL, pszCommand); 

    Delay(0.1);

    dStartTime = Timer(); 

    while (1)
    {
    	InetTelnetRead (g_hShellCommand, szTemp, sizeof(szTemp)-1, &iByte, 100);
    	strcat(szBuffer, szTemp);
    	iPos = FindPattern (szBuffer, 0, -1, "...", 0, 0);	   //Welcome to
        if (iByte == 0 && iTotalBytesRead > 0 && iPos >= 0)
            break;
        iTotalBytesRead += iByte;
    	
       dTimeElapsed = Timer() - dStartTime;
       if( dTimeElapsed > iTimeout ) 
       {
    	   sprintf(szLogMsg,"CMON Agent Timeout...");
    	   iResult = -1;
    	   goto Error;
       }
    	
    	
    }
    EELog_INEventLogLogEvent (g_hEventLog, NULL, szBuffer);



    iPos = FindPattern(szBuffer ,0,-1,"error",0,0); 
    if(iPos >= 0) 
    { 
       sprintf( szLogMsg,"%s","CMON Agent Error Occured.");	   
       *PassFail = 0;
       iResult = -1;
       goto Error;
    } 

    iPos = FindPattern (szBuffer, 0, -1, "FAIL", 0, 0);
    if(iPos>=0)
    {
       sprintf(szLogMsg,"%s", "CMON Agent Executed Result Fail.");
       *PassFail = 0;
       iResult = -1;
       
    } 
    else
    {

       iPos = FindPattern (szBuffer, 0, -1, pszPattern, 0, 0);
    	   if(iPos>=0)
       {
    	   sprintf(szLogMsg,"%s", "Command Executed Sucessfully.");
    	   EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
    	   
    	   *PassFail = 1;
    	   iResult = 0;
       }
       else
       {
    	   sprintf(szLogMsg,"%s", "Command Executed Sucessfully but .");
    	   EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
    	   *PassFail = 0;
    	   iResult = -1;
    	   }
    }



Error:	

    if(iResult <0 )
    {
         EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
        *PassFail = -999;
    }

    return iResult;


}


// -----------------------------------------------------------------------
// Function:   ShellCommand_String  
// Purpose:               . 
//             
// Parameters: 
int ShellCommand_String(char* pszCommand, int iTimeout,char* pszPattern, int* PassFail)
{


    int iByte = 0;
    int	iPos = -1;
    int iTotalBytesRead = 0;
    int iResult = 0;
    char szBuffer[1024] = {'\0'};
    char szTemp[512] = {'\0'};

    double dTimeElapsed = 0.0;
    double dStartTime = 0.0;

    ErrMsg szLogMsg;


    if(iTimeout < 0)
    {
    	iTimeout = 20;	//second
    }

    InetTelnetWrite (g_hShellCommand, pszCommand, "\r\n", -1, &iByte, T25000MS);
    EELog_INEventLogLogEvent (g_hEventLog, NULL, pszCommand); 

    Delay(0.1);

    dStartTime = Timer(); 

    while (1)
    {
    	InetTelnetRead (g_hShellCommand, szTemp, sizeof(szTemp)-1, &iByte, 100);
    	strcat(szBuffer, szTemp);
    	iPos = FindPattern (szBuffer, 0, -1,"$", 0, 0);	   //Welcome to
        if (iByte == 0 && iTotalBytesRead > 0 && iPos >= 0)
            break;
        iTotalBytesRead += iByte;
    	
       dTimeElapsed = Timer() - dStartTime;
       if( dTimeElapsed > iTimeout ) 
       {
    	   sprintf(szLogMsg,"CMON Agent Timeout...");
    	   iResult = -1;
    	   goto Error;
       }
    	
    	
    }
    EELog_INEventLogLogEvent (g_hEventLog, NULL, szBuffer);



    iPos = FindPattern(szBuffer ,0,-1,"error",0,0); 
    if(iPos >= 0) 
    { 
       sprintf( szLogMsg,"%s","CMON Agent Error Occured.");	   
       *PassFail = 0;
       iResult = -1;
       goto Error;
    } 

    iPos = FindPattern (szBuffer, 0, -1, "FAIL", 0, 0);
    if(iPos>=0)
    {
       sprintf(szLogMsg,"%s", "CMON Agent Executed Result Fail.");
       *PassFail = 0;
       iResult = -1;
       
    } 
    else
    {

       iPos = FindPattern (szBuffer, 0, -1, pszPattern, 0, 0);
    	   if(iPos>=0)
       {
    	   sprintf(szLogMsg,"%s", "Command Executed Sucessfully.");
    	   EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
    	   
    	   *PassFail = 1;
    	   iResult = 0;
       }
       else
       {
    	   sprintf(szLogMsg,"%s", "Command Executed Sucessfully but .");
    	   EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
    	   *PassFail = 0;
    	   iResult = -1;
    	   }
    }



Error:	

    if(iResult <0 )
    {
         EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
        *PassFail = -999;
    }

    return iResult;


}

// -----------------------------------------------------------------------
// Function:   Shmon_GetStringAccordingToPattern  
// Purpose:               . 
//             
// Parameters: 
int Shmon_GetStringAccordingToPattern(char* pszCommand, int iTimeout,_SHMON_FIND_PATTERN_GET_SRING* pszFindPattern, int* PassFail)
{


    int iByte = 0;
    int	iPos = -1;
    int iTotalBytesRead = 0;
    int iResult = 0;
    char szBuffer[1024] = {'\0'};
    char szTemp[512] = {'\0'};    

    char* ptr = NULL;
    char szTempGetString[BUFFER_SIZE_64] = {'\0'};
    int iGetStringLength = 0;
    
    double dTimeElapsed = 0.0;
    double dStartTime = 0.0;

    ErrMsg szLogMsg;


    if(iTimeout < 0)
    {
    	iTimeout = 20;	//second
    }

    InetTelnetWrite (g_hShellCommand, pszCommand, "\r\n", -1, &iByte, T25000MS);
    EELog_INEventLogLogEvent (g_hEventLog, NULL, pszCommand); 

    Delay(0.1);

    dStartTime = Timer(); 

    while (1)
    {
    	InetTelnetRead (g_hShellCommand, szTemp, sizeof(szTemp)-1, &iByte, 100);
    	strcat(szBuffer, szTemp);
    	iPos = FindPattern (szBuffer, 0, -1,"$", 0, 0);	   //Welcome to
        if (iByte == 0 && iTotalBytesRead > 0 && iPos >= 0)
            break;
        iTotalBytesRead += iByte;
    	
       dTimeElapsed = Timer() - dStartTime;
       if( dTimeElapsed > iTimeout ) 
       {
    	   sprintf(szLogMsg,"CMON Agent Timeout...");
    	   iResult = -1;
    	   goto Error;
       }
    	
    	
    }
    EELog_INEventLogLogEvent (g_hEventLog, NULL, szBuffer);



    iPos = FindPattern(szBuffer ,0,-1,"error",0,0); 
    if(iPos >= 0) 
    { 
       sprintf( szLogMsg,"%s","CMON Agent Error Occured.");	   
       *PassFail = 0;
       iResult = -1;
       goto Error;
    } 

    iPos = FindPattern (szBuffer, 0, -1, "FAIL", 0, 0);
    if(iPos>=0)
    {
       sprintf(szLogMsg,"%s", "CMON Agent Executed Result Fail.");
       *PassFail = 0;
       iResult = -1;
       
    } 
    else
    {
       
       iPos = FindPattern (szBuffer, 0, -1, pszFindPattern->cFindPattern, 0, 0);
       if(iPos < 0)
       {
           sprintf(szLogMsg,"%s", "Command Executed Sucessfully but .");
           EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
           *PassFail = 0;
           iResult = -1;
       }
       else
       {
           ptr = strstr(szBuffer, pszFindPattern->cFindPattern);
           if(NULL == ptr)
           {
               return  -1;
           }
           
           //Get the String address
           ptr = ptr + pszFindPattern->iOffsetFromPattern;

           iGetStringLength = strlen(pszFindPattern->cGetString);
           if(0 >= iGetStringLength)
           {
               return  -1;
           }
           
           strncpy(szTempGetString,ptr,iGetStringLength);
           szTempGetString[iGetStringLength + 1] = '\0';
           
           if(0 != strcmp(szTempGetString,pszFindPattern->cGetString))
           {
               sprintf( szLogMsg,"Cant find the pattern :%s string.",pszFindPattern->cGetString);  
               EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);            
               *PassFail = 0;
               iResult = -1;

               goto Error;
           }
            iResult = 0; 
       }
   }

Error:	

    if(iResult <0 )
    {
         EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
        *PassFail = -999;
    }

    return iResult;


}

// -----------------------------------------------------------------------
// Function:   Shmon_GetStringAccordingToPattern  
// Purpose:               . 
//             
// Parameters: 
int Shmon_GetNumericAccordingToPattern(char* pszCommand, int iTimeout,_SHMON_FIND_PATTERN_GET_NUMERIC* pszFindPattern, int* PassFail)
{


    int iByte = 0;
    int	iPos = -1;
    int iTotalBytesRead = 0;
    int iResult = 0;
    char szBuffer[1024] = {'\0'};
    char szTemp[512] = {'\0'};    

    char* ptr = NULL;
    int iGetStringLength = 0;
    int i = 0;
    
    double dTimeElapsed = 0.0;
    double dStartTime = 0.0;

    ErrMsg szLogMsg;


    if(iTimeout < 0)
    {
    	iTimeout = 20;	//second
    }

    InetTelnetWrite (g_hShellCommand, pszCommand, "\r\n", -1, &iByte, T25000MS);
    EELog_INEventLogLogEvent (g_hEventLog, NULL, pszCommand); 

    Delay(0.1);

    dStartTime = Timer(); 

    while (1)
    {
    	InetTelnetRead (g_hShellCommand, szTemp, sizeof(szTemp)-1, &iByte, 100);
    	strcat(szBuffer, szTemp);
    	iPos = FindPattern (szBuffer, 0, -1,"$", 0, 0);	   //Welcome to
        if (iByte == 0 && iTotalBytesRead > 0 && iPos >= 0)
            break;
        iTotalBytesRead += iByte;
    	
       dTimeElapsed = Timer() - dStartTime;
       if( dTimeElapsed > iTimeout ) 
       {
    	   sprintf(szLogMsg,"CMON Agent Timeout...");
    	   iResult = -1;
    	   goto Error;
       }
    	
    	
    }
    EELog_INEventLogLogEvent (g_hEventLog, NULL, szBuffer);



    iPos = FindPattern(szBuffer ,0,-1,"error",0,0); 
    if(iPos >= 0) 
    { 
       sprintf( szLogMsg,"%s","CMON Agent Error Occured.");	   
       *PassFail = 0;
       iResult = -1;
       goto Error;
    } 

    iPos = FindPattern (szBuffer, 0, -1, "FAIL", 0, 0);
    if(iPos>=0)
    {
       sprintf(szLogMsg,"%s", "CMON Agent Executed Result Fail.");
       *PassFail = 0;
       iResult = -1;
       
    } 
    else
    {
       
       iPos = FindPattern (szBuffer, 0, -1, pszFindPattern->cFindPattern, 0, 0);
       if(iPos < 0)
       {
           sprintf(szLogMsg,"%s", "Command Executed Sucessfully but .");
           EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
           *PassFail = 0;
           iResult = -1;
       }
       else
       {
        	ptr = strstr(szBuffer, pszFindPattern->cFindPattern);
        	if(NULL == ptr)
        	{
        	    return  -1;
            }

        	ptr = ptr + strlen(pszFindPattern->cFindPattern);
            
            i = 0;
        	while (*ptr != '\0' && ((*ptr >= 48 && *ptr <= 57) || *ptr == 45 || *ptr == 46 || *ptr == 32))
        	{
        		szTemp[i] = *ptr;

        		++ptr;
        		++i;
                if(i > MAXGETBYTE)
                {
                    break;
                }
        	}
            
        	szTemp[i] = '\0';
            
        	pszFindPattern->cGetNumeric = atof(szTemp);
                      
            iResult = 0;            
        }
   }

Error:	

    if(iResult <0 )
    {
         EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
        *PassFail = -999;
    }

    return iResult;


}


// -----------------------------------------------------------------------
// Function:   CmonAgent_PassFail  
// Purpose:               . 
//             
// Parameters: 
int ShellCommand_PassFail(char* pszCommand, int iTimeout, int* PassFail)
{


    int iByte = 0;
    int	iPos = -1;
    int iTotalBytesRead = 0;
    int iResult = 0;
    char szBuffer[1024] = {'\0'};
    char szTemp[512] = {'\0'};

    double dTimeElapsed = 0.0;
    double dStartTime = 0.0;

    ErrMsg szLogMsg;


    if(iTimeout < 0)
    {
    	iTimeout = 20;	//second
    }

    InetTelnetWrite (g_hShellCommand, pszCommand, "\r\n", -1, &iByte, T25000MS);
    EELog_INEventLogLogEvent (g_hEventLog, NULL, pszCommand); 

    Delay(0.1);

    dStartTime = Timer(); 

    while (1)
    {
    	InetTelnetRead (g_hShellCommand, szTemp, sizeof(szTemp)-1, &iByte, 100);
    	strcat(szBuffer, szTemp);
    	iPos = FindPattern (szBuffer, 0, -1, "ret", 0, 0);
        if (iByte == 0 && iTotalBytesRead > 0 && iPos >= 0)
            break;
        iTotalBytesRead += iByte;
    	
       dTimeElapsed = Timer() - dStartTime;
       if( dTimeElapsed > iTimeout ) 
       {
    	   sprintf(szLogMsg,"CMON Agent Timeout...");
    	   iResult = -1;
    	   goto Error;
       }
    	
    	
    }
    EELog_INEventLogLogEvent (g_hEventLog, NULL, szBuffer);



    iPos = FindPattern(szBuffer ,0,-1,"error",0,0); 
    if(iPos >= 0) 
    { 
       sprintf( szLogMsg,"%s","CMON Agent Error Occured.");	   
       iResult = -1;
       goto Error;
    } 

    iPos = FindPattern (szBuffer, 0, -1, "FAIL", 0, 0);
    if(iPos>=0)
    {
       sprintf(szLogMsg,"%s", "CMON Agent Executed Result Fail.");
       *PassFail = 0;
       iResult = -1;
       
    } 
    else
    {

       sprintf(szLogMsg,"%s", "Command Executed Sucessfully.");
       EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
       *PassFail = 1;
       iResult = 0;

    }



Error:	

    if(iResult <0 )
    {
         EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
        *PassFail = -999;
    }

    return iResult;


}

// -----------------------------------------------------------------------
// Function:  ShellCommand_Action  
// Purpose:               . 
//             
// Parameters: 
//


int Shmon_SendAndAck(char* pszCommand, int iTimeout,const char* pszAck)
{


    int iByte = 0;
    int	iPos = -1;
    int iTotalBytesRead = 0;
    int iResult = 0;
    char szBuffer[1024] = {'\0'};
    char szTemp[512] = {'\0'};

    double dTimeElapsed = 0.0;
    double dStartTime = 0.0;

    ErrMsg szLogMsg;


    if(iTimeout < 0)
    {
    	iTimeout = 20;	//second
    }

    // Clear the welcome information
    InetTelnetRead (g_hShellCommand, szTemp, sizeof(szTemp)-1, &iByte, 100);
    iByte = 0;
    *szTemp = '\0';


    InetTelnetWrite (g_hShellCommand, pszCommand, "\r\n", -1, &iByte, T25000MS);
    EELog_INEventLogLogEvent (g_hEventLog, NULL, pszCommand); 

    Delay(0.1);

    dStartTime = Timer(); 

    while (1)
    {
    	InetTelnetRead (g_hShellCommand, szTemp, sizeof(szTemp)-1, &iByte, 100);
    	strcat(szBuffer, szTemp);
    	iPos = FindPattern (szBuffer, 0, -1, pszAck, 0, 0);
        if (iByte == 0 && iTotalBytesRead > 0 && iPos >= 0)
            break;
        iTotalBytesRead += iByte;
    	
        dTimeElapsed = Timer() - dStartTime;
        if( dTimeElapsed > iTimeout) 
        {
    		   sprintf(szLogMsg,"ShellCommand Timeout...");
    		   iResult = -1;
    		   goto Error;
        }
    	
    }
    EELog_INEventLogLogEvent (g_hEventLog, NULL, szBuffer); 

Error:	

    if(iResult <0 )
    {
         EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
    }

    return iResult;


}

// -----------------------------------------------------------------------
// Function:  Shmon_RunScript  
// Purpose:               . 
//             
// Parameters: 
//


int Shmon_RunScript(char* pszCommand, int iTimeout)
{


    int iByte = 0;
    int	iPos = -1;
    int iTotalBytesRead = 0;
    int iResult = 0;
    char szBuffer[1024] = {'\0'};
    char szTemp[512] = {'\0'};

    double dTimeElapsed = 0.0;
    double dStartTime = 0.0;

    ErrMsg szLogMsg;


    if(iTimeout < 0)
    {
    	iTimeout = 20;	//second
    }

    // Clear the welcome information
    InetTelnetRead (g_hShellCommand, szTemp, sizeof(szTemp)-1, &iByte, 100);

    InetTelnetWrite (g_hShellCommand, pszCommand, "\r\n", -1, &iByte, T25000MS);
    EELog_INEventLogLogEvent (g_hEventLog, NULL, pszCommand); 

    Delay(0.1);

    dStartTime = Timer(); 

    while (1)
    {
    	InetTelnetRead (g_hShellCommand, szTemp, sizeof(szTemp)-1, &iByte, 100);
    	
        dTimeElapsed = Timer() - dStartTime;
        if( dTimeElapsed > iTimeout) 
        {
    		   sprintf(szLogMsg,"shell command script %s run completely",pszCommand);
               EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
               break;
        }
    	
    }

    return 0;
}

// -----------------------------------------------------------------------
// Function:  Shmon_RunCmdNoAck  
// Purpose:               . 
//             
// Parameters: 
//


int Shmon_RunCmdNoAck(char* pszCommand, int iTimeout)
{


    int iByte = 0;
    int	iPos = -1;
    int iTotalBytesRead = 0;
    int iResult = 0;
    char szBuffer[1024] = {'\0'};
    char szTemp[512] = {'\0'};

    double dTimeElapsed = 0.0;
    double dStartTime = 0.0;

    ErrMsg szLogMsg;


    if(iTimeout < 0)
    {
    	iTimeout = 20;	//second
    }

    // Clear the welcome information
    InetTelnetRead (g_hShellCommand, szTemp, sizeof(szTemp)-1, &iByte, 100);

    InetTelnetWrite (g_hShellCommand, pszCommand, "\r\n", -1, &iByte, T25000MS);
    EELog_INEventLogLogEvent (g_hEventLog, NULL, pszCommand); 

    Delay(0.1);

    dStartTime = Timer(); 

    while (1)
    {
    	InetTelnetRead (g_hShellCommand, szTemp, sizeof(szTemp)-1, &iByte, 100);
    	
        dTimeElapsed = Timer() - dStartTime;
        if( dTimeElapsed > iTimeout) 
        {
    		   sprintf(szLogMsg,"shell command %s run completely",pszCommand);
               EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
               break;
        }
    	
    }

    return 0;
}

// -----------------------------------------------------------------------
// Function:   ShellCommandWriteReadUntil  
// Purpose:               . 
//             
// Parameters: 
int ShellCommandWriteReadUntil(char* pszCommand,char* pszSearchString,char* pszReturnString, int iTimeOut,int iPrintLog  )
{

    int error = -1;

    //  char szOseScriptLog[10240] = {'\0'};
    int  iBytesWritten  = 0;
    int  iBytesRead = 0;
    int  iPos = -1;

    char szLogMsg[BUF_SIZE_HUGE+1] = {'\0'}; 
    // char szTemp[10240] = {'\0'};
    if (iPrintLog == 1) {
      sprintf(szLogMsg,"%s UNIX:%s","->",pszCommand);
      EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
    }
    error = InetTelnetWrite (g_hShellCommand, pszCommand, "\r\n", -1, &iBytesWritten, 5000); 
    if (error !=0) {
      goto Error;
    }
    strcpy(pszReturnString,""); 
    if (StringLength (pszSearchString) > 0) {

    //Not ready RiM
     //gives non-fatal runtime error if searchString does not exist
     //error = InetTelnetReadUntil (g_hUnixShell,pszReturnString ,BUF_SIZE_32K , pszSearchString, iTimeOut);
     
     while (iPos < 0) {
        error = InetTelnetRead (g_hShellCommand,pszReturnString ,BUF_SIZE_32K,&iBytesRead, 10);
        iPos = FindPattern (pszReturnString, 0, -1, pszSearchString, 1, 0);
        iTimeOut = iTimeOut - 10;
        if (iTimeOut <= 0)  {
            error = -1;
			break;
        }
      }

    }
    else {
      error = InetTelnetRead (g_hShellCommand,pszReturnString ,BUF_SIZE_32K,&iBytesRead, iTimeOut);
    }  
    //   iPos = FindPattern (pszReturnString, 0, -1, "\n", 1, 0);  

    //   CopyString (szTemp, 0, pszReturnString, iPos, -1);


Error:
    if (iPrintLog == 1 || error == -1) {
      sprintf(szLogMsg,"%s UNIX:%s","->",pszCommand);
      EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
      
      sprintf(szLogMsg,"%s UNIX:%s, Bytes: %d, Timeout:%d","<-",pszReturnString, iBytesRead,iTimeOut);
      EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
    }

    return error;   

}
//---------------------------
// ======================================================================================
// Function:   FRmonAgent_String
// Purpose:    
// Parameters: 

int FRmonAgent_String(char* pszCommand, int iTimeout, char* pszPattern, int* PassFail)
{


    int iByte = 0;
    int	iPos = -1;
    int iTotalBytesRead = 0;
    int iResult = 0;
    char szBuffer[BUF_SIZE_32K+1] = {'\0'};
    char szTemp[BUF_SIZE_32K+1] = {'\0'};

    double dTimeElapsed = 0.0;
    double dStartTime = 0.0;

    ErrMsg szLogMsg;


    if(iTimeout < 0)
    {
    	iTimeout = 10;	//second
    }

    InetTelnetWrite (g_hFrmonAgent, pszCommand, "\r\n", -1, &iByte, T25000MS);
    EELog_INEventLogLogEvent (g_hEventLog, NULL, pszCommand); 

    Delay(0.1);

    dStartTime = Timer(); 

    while (1)
    {
    	InetTelnetRead (g_hFrmonAgent, szTemp, sizeof(szTemp)-1, &iByte, 100);
    	strcat(szBuffer, szTemp);
    	iPos = FindPattern (szBuffer, 0, -1, "ret", 0, 0);
        if (iByte == 0 && iTotalBytesRead > 0 && iPos >= 0)
            break;
        iTotalBytesRead += iByte;
    	
        dTimeElapsed = Timer() - dStartTime;
        if( dTimeElapsed > iTimeout ) 
        {
           sprintf(szLogMsg,"FRMON Agent Timeout...");
           iResult = -1;
           goto Error;
        }	
    }
    
    EELog_INEventLogLogEvent (g_hEventLog, NULL, szBuffer);



    iPos = FindPattern(szBuffer ,0,-1,"NOK",0,0); 
    if(iPos >= 0) 
    { 
       sprintf( szLogMsg,"%s","FRMON Agent Error Occured.");	   
       *PassFail = 0;
       iResult = 0;
       goto Error;
    } 

    iPos = FindPattern (szBuffer, 0, -1, "NOK", 0, 0);
    if(iPos>=0)
    {
       sprintf(szLogMsg,"%s", "FRMON Agent Executed Result Fail.");
       *PassFail = 0;
       iResult = 0;
       
    } 
    else
    {

       iPos = FindPattern (szBuffer, 0, -1, pszPattern, 0, 0);
       if(iPos>=0)
       {
    	   sprintf(szLogMsg,"%s", "Command Executed Sucessfully.");
    	   EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
    	   
    	   *PassFail = 1;
    	   iResult = 0;
       }
       else
       {
    	   sprintf(szLogMsg,"%s", "Command Executed Sucessfully but .");
    	   EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
    	   *PassFail = 0;
    	   iResult = 0;
       }
    }

Error:	

    if(iResult < 0)
    {
         EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
        *PassFail = -999;
    }

    return iResult; 
}

// ======================================================================================
// Function:   FRmonAgentWriteReadUntil
// Purpose:    
// Parameters: 

int FRmonAgentWriteReadUntil( char* pszCommand,char* pszSearchString,char*  pszReturnString, long iTimeOut,int iPrintLog  )
{
    int error = -1;

    //  char szOseScriptLog[10240] = {'\0'};
    int  iBytesWritten  = 0;
    int  iBytesRead = 0;
    int  iPos = -1;

    char szLogMsg[64000] = {'\0'}; 
    // char szTemp[10240] = {'\0'};

    if (iPrintLog == 1) {
      sprintf(szLogMsg,"%s FRmonAgent:%s","->",pszCommand);
      EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
    }
    error = InetTelnetWrite (g_hFrmonAgent, pszCommand, "\r\n", -1, &iBytesWritten, 5000); 
    if (error !=0) {
      goto Error;
    }
    
    strcpy(pszReturnString,""); 
    if (StringLength (pszSearchString) > 0) 
    {

     //Not ready RiM
      //gives non-fatal runtime error if searchString does not exist
      //error = InetTelnetReadUntil (g_hOseShell,kszReturnString ,BUF_SIZE_32K , pszSearchString, iTimeOut);
      
      while (iPos < 0) 
      {
        error = InetTelnetRead (g_hFrmonAgent,pszReturnString ,BUF_SIZE_32K,&iBytesRead, 10);

        iPos = FindPattern (pszReturnString, 0, -1, "NOK", 0, 0);
        if(iPos>=0)
        {
            error = -2;
            break;
        } 
        iPos = FindPattern (pszReturnString, 0, -1, pszSearchString, 1, 0);
        iTimeOut = iTimeOut - 10;
        if (iTimeOut <= 0)  
        {
            error = -1;
			break;
        }
      }

    }
    else 
    {
      error = InetTelnetRead (g_hFrmonAgent,pszReturnString ,BUF_SIZE_32K,&iBytesRead, iTimeOut);
    }  
    //   iPos = FindPattern (pszReturnString, 0, -1, "\n", 1, 0);  

    //  CopyString (szTemp, 0, pszReturnString, iPos, -1);


Error:
	if (iPrintLog == 1 || error == -1) {
	  sprintf(szLogMsg,"%s TRX:%s","->",pszCommand);
	  EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
	  
	  sprintf(szLogMsg,"%s TRX:%s, Bytes: %d, Timeout:%d","<-",pszReturnString, iBytesRead,iTimeOut);
	  EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
	}
    else if(error == -2)
    {
        sprintf(szLogMsg,"%s", "FRMON Agent Executed Result Fail.");
        EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
        
        sprintf(szLogMsg,"%s TRX:%s, Bytes: %d, Timeout:%d","<-",pszReturnString, iBytesRead,iTimeOut);
	    EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
    }
    else
    {
        ;
    }
    return error;   
}

// -----------------------------------------------------------------------
// Function:  Frmon_RunCmdNoAck  
// Purpose:               . 
//             
// Parameters: 
//


int Frmon_RunCmdNoAck(char* pszCommand, int iTimeout)
{


    int iByte = 0;
    int	iPos = -1;
    int iTotalBytesRead = 0;
    int iResult = 0;
    char szBuffer[1024] = {'\0'};
    char szTemp[512] = {'\0'};

    double dTimeElapsed = 0.0;
    double dStartTime = 0.0;

    ErrMsg szLogMsg;


    if(iTimeout < 0)
    {
    	iTimeout = 20;	//second
    }

    // Clear the welcome information
    InetTelnetRead (g_hFrmonAgent, szTemp, sizeof(szTemp)-1, &iByte, 100);

    InetTelnetWrite (g_hFrmonAgent, pszCommand, "\r\n", -1, &iByte, T25000MS);
    EELog_INEventLogLogEvent (g_hEventLog, NULL, pszCommand); 

    Delay(0.1);

    dStartTime = Timer(); 

    while (1)
    {
    	InetTelnetRead (g_hFrmonAgent, szTemp, sizeof(szTemp)-1, &iByte, 100);
    	
        dTimeElapsed = Timer() - dStartTime;
        if( dTimeElapsed > iTimeout) 
        {
    		   sprintf(szLogMsg,"shell command %s run completely",pszCommand);
               EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
               break;
        }
    	
    }

    return 0;
}

int ShellCmdWriteAndCompResult(const char *cShellCmd,char *cResult,int parm1)
{    
    char kszCmonAgent[BUFFER_SIZE_128] = {'\0'};
    int error = 0;
    int vPassFail = 0;

    if((NULL == cShellCmd) || (NULL == cResult))
    {
        return 0;
    }

    sprintf(kszCmonAgent,cShellCmd,parm1);//select channel
    ErrChk(ShellCommand_String(kszCmonAgent, -1,cResult, &vPassFail));

Error:    
    return vPassFail;
}

int ShellCmdReadAndGetReturnString(const char *cShellCmd,char* cSearchString,char *cReturnString,int TimeoutMs)
{    
    ErrMsg errMsg = {'\0'};
    char kszCmonAgent[BUFFER_SIZE_128] = {'\0'};
    int vPassFail = 0;

    if((NULL == cShellCmd) || (NULL == cSearchString) || (NULL == cReturnString))
    {
        return -1;
    }
        
    sprintf(kszCmonAgent,cShellCmd);
    if(ShellCommandWriteReadUntil(kszCmonAgent,cSearchString,cReturnString,TimeoutMs,1) !=0 )
    {   
        sprintf(errMsg, STRING_MESSAGE_Shell_Write_Read_Fail, kszCmonAgent);
    }

    return -1;
}
int RfbbFTPDownloadFromRemote(const char *localFile,const char *remoteFile)
{
    int iReturn = 0;  

    iReturn = InetFTPAutoRetrieve (g_szDUTIP, "sw", "", localFile, remoteFile, INET_FTP_FILE_TYPE_BINARY);

    return iReturn;
}
int RfbbInetTelnetOpen (const char *telnetServer, unsigned short remotePort,
                            unsigned short localPort)
{
    int iReturn = 0;  

    iReturn = InetTelnetOpen (telnetServer, remotePort,
                            localPort);

    return iReturn;
}
int RfbbInetFTPAutoSend (const char *ftpServer, const char *userName,
                             const char *password, const char *localFile,
                             const char *remoteFile, int transferType)
{
    int iReturn = 0;  

    iReturn = InetFTPAutoSend (ftpServer, userName,
                             password, localFile,
                             remoteFile, transferType);

    return iReturn;
}

#else
void __decldllexp TX_TEST InitialiseInstruments(tTestData *testData, tTestError *testError)
 {
    return;    
 }
void __decldllexp TX_TEST CleanupInstruments(tTestData *testData, tTestError *testError)
 {
     return;
 }
 void __decldllexp TX_TEST PowerUpDut(tTestData *testData,  tTestError *testError)
 {      
    return;  
 }                                                                            
 void __decldllexp TX_TEST PowerDownDut(tTestData *testData, tTestError *testError)
 {       
    return;
 }
 void __decldllexp TX_TEST PowerUpPSU(tTestData *testData, tTestError *testError, char *pszPSU)
 {      
    return;  
 }
 void __decldllexp TX_TEST PowerDownPSU(tTestData *testData, tTestError *testError, char *pszPSU)
 {
    return;
 }
 void __decldllexp TX_TEST IsFixtureReady(tTestData *testData, tTestError *testError)
 {       
    return;    
 }
 int CmonAgent_Action(char* pszCommand, int iTimeout)
 {
     return 1;        
 }
 int CmonAgent_PassFail(char* pszCommand, int iTimeout, int* PassFail)
 {   
    *PassFail = 1;
     return 1;        
 }
 int CmonAgent_Numeric(char* pszCommand, int iTimeout, char* pszPattern, double* dMeasurement)
 {
     return 1;        
 }    
 int CmonAgent_String(char* pszCommand, int iTimeout, char* pszPattern, int* PassFail)
 {       
     *PassFail = 1;
     return 1; 
 }   
 int CmonAgentWriteReadUntil( char* pszCommand,char* pszSearchString,char*  pszReturnString, long iTimeOut,int iPrintLog  )
 {    
    return 0;   
 }
 int ShellCommand_Numeric(char* pszCommand, int iTimeout, char* pszPattern, double* dMeasurement)
 {
     return 1;  
     
 }    
 int ShellCommand_Action(char* pszCommand, int iTimeout)
 {
     return 1;        
 }    
 int ShellCommand_Hex(char* pszCommand, int iTimeout, char* pszPattern, char* pszHex)
 {          
     return 0;        
 }     
 int ShellCommand_StringSpecial(char* pszCommand, int iTimeout, char* pszPattern, int* PassFail)
 {       
     *PassFail = 1;
     return 0;
 }
 int ShellCommand_String(char* pszCommand, int iTimeout, char* pszPattern, int* PassFail)
 {
    *PassFail = 1;
    return 1;
 }
 int ShellCommand_PassFail(char* pszCommand, int iTimeout, int* PassFail)
 {
    *PassFail = 1;
     return 0;        
 }
 int ShellCommandWriteReadUntil(char* pszCommand,char* pszSearchString,char* pszReturnString, int iTimeOut,int iPrintLog  )
 {
     return 0;
 }
 
int ShellCmdReadAndGetReturnString(const char *cShellCmd,char* cSearchString,char *cReturnString,int TimeoutMs)
{  
    return 0;
}
int RfbbFTPDownloadFromRemote(const char *localFile,const char *remoteFile) 
{    
    return 1;
}
int RfbbInetTelnetOpen (const char *telnetServer, unsigned short remotePort,
                            unsigned short localPort)
{
    return 0;
}
int RfbbInetFTPAutoSend (const char *ftpServer, const char *userName,
                             const char *password, const char *localFile,
                             const char *remoteFile, int transferType)
{
    return 0;
}

int CmonAgentMultiNumeric(char* pszCommand, int iTimeout, _FIND_PATTERN_AND_NUMERIC* pszFindPattern, int iTotlePatternNum)
{


    int iByte = 0;
    int	iPos = -1;
    int iTotalBytesRead = 0;
    int iPatternNum = 0;
    int iResult = 0;
    char szBuffer[BUF_SIZE_32K+1] = "ret target/bist/action bist_tmp_sensor 0x00 BB Board Register R/W Test PASS. BB Board Temperature 63.00 Celsius. TRX 1 Register R/W Test PASS. TRX 1 Temperature 66.00 Celsius. TRX 2 Register R/W Test PASS. TRX 2 Temperature 69.50 Celsius. TRX 3 Register R/W Test PASS. TRX 3 Temperature 71.50 Celsius. TRX 4 Register R/W Test PASS. TRX 4 Temperature 70.00 Celsius. ";
            //FBRX1 Register R/W Test PASS. FBRX1 Temperature 68.50 Celsius. FBRX2 Register R/W Test PASS. FBRX2 Temperature UNKNOWN Celsius.";

    char szTemp[BUF_SIZE_32K+1] = {'\0'};
    char* ptr = 0;
    int i = 0;
    double dTimeElapsed = 0.0;
    double dStartTime = 0.0;
    _FIND_PATTERN_AND_NUMERIC* ptrTempPattern = NULL;

    ErrMsg szLogMsg;

    if((iTotlePatternNum <= 0) || (pszFindPattern == NULL))
    {
        return -1;
    }
    
    if(iTimeout < 0)
    {
    	iTimeout = 10;	//second
    }

    //InetTelnetWrite (g_hCmonAgent, pszCommand, "\r\n", -1, &iByte, T25000MS);
    EELog_INEventLogLogEvent (g_hEventLog, NULL, pszCommand); 

    Delay(0.1);

    //dStartTime = Timer(); 

    //while (1)
    //{
    	//InetTelnetRead (g_hCmonAgent, szTemp, sizeof(szTemp)-1, &iByte, 100);
    	//strcat(szBuffer, szTemp);
    	iPos = FindPattern (szBuffer, 0, -1, "ret", 0, 0);
        //if (iByte == 0 && iTotalBytesRead > 0 && iPos >= 0)
        //    break;
        //iTotalBytesRead += iByte;
    	
        //dTimeElapsed = Timer() - dStartTime;
        //if( dTimeElapsed > iTimeout ) 
        //{
           //sprintf(szLogMsg,"CMON Agent Timeout...");
           //iResult = -1;
           //goto Error;
        //}	
    //}
    
    EELog_INEventLogLogEvent (g_hEventLog, NULL, szBuffer);
    
    for(iPatternNum = 0;iPatternNum < iTotlePatternNum;iPatternNum++)
    {
        ptrTempPattern = (_FIND_PATTERN_AND_NUMERIC*)(pszFindPattern + iPatternNum);
        iPos = FindPattern(szBuffer ,0,-1,ptrTempPattern->cPatternString,0,0); 
        if(iPos < 0) 
        { 
           sprintf( szLogMsg,"Cant find the pattern :%s string and numeric.",ptrTempPattern->cPatternString);	
           EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
           iResult = -1;
           
           goto Error;
        }
        else
        {
        	ptr = strstr(szBuffer, ptrTempPattern->cPatternString);
        	if(NULL == ptr)
        	{
        	    return  -1;
            }

        	ptr = ptr + strlen(ptrTempPattern->cPatternString);
            i = 0;
        	while (*ptr != '\0' && ((*ptr >=48 && *ptr <=57) || *ptr == 45 || *ptr == 46 || *ptr == 32))
        	{
        		szTemp[i] = *ptr;

        		++ptr;
        		++i;
        	}
            
        	szTemp[i] = '\0';
            
        	ptrTempPattern->cFindNumeric = atof(szTemp);
           
           
           iResult = 0;            
        }
    }
 

Error:	

    if(iResult <0)
    {
         EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);        
    }

return iResult; 
}
// ======================================================================================
// Function:   CmonAgent_String
// Purpose:    
// Parameters:

int CmonAgentMultiString(char* pszCommand, int iTimeout, _FIND_PATTERN_GET_SRING* pszFindPattern, int iTotlePatternNum,int* PassFail)
{


    int iByte = 0;
    int	iPos = -1;
    int iTotalBytesRead = 0;
    int iPatternNum = 0;
    int iGetStringLength = 0;
    int iResult = 0;
    int i = 0;
    char szBuffer[BUF_SIZE_32K+1] = "ret target/bist/action bist_status 0x00 CSSA ID, expected 0x00000002, PASS.CSSA Register RW Test PASS.CSSA Reset Test PASS. MURKKU ID, expected 0x00000002, PASS. MURKKU Register RW Test PASS. MURKKU Reset Test PASS. IFFPGA ID, expected 0x38504950, PASS. IFFPGA Register RW Test PASS. IFFPGA Reset Test PASS. DSP ID, expected 0x4e840102, PASS. DSP Register RW Test PASS. DSP Reset Test PASS.";
    char szTemp[BUF_SIZE_32K+1] = {'\0'};
    char* ptr = NULL;
    char szTempGetString[BUFFER_SIZE_64] = {'\0'};
    _FIND_PATTERN_GET_SRING* ptrTempPattern = NULL;

    double dTimeElapsed = 0.0;
    double dStartTime = 0.0;

    ErrMsg szLogMsg;

    if((iTotlePatternNum <= 0) || (pszFindPattern == NULL))
    {
        return -1;
    }
    
    if(iTimeout < 0)
    {
    	iTimeout = 10;	//second
    }

    //InetTelnetWrite (g_hCmonAgent, pszCommand, "\r\n", -1, &iByte, T25000MS);
    //EELog_INEventLogLogEvent (g_hEventLog, NULL, pszCommand); 

    Delay(0.1);

    //dStartTime = Timer(); 

    //while (1)
    //{
    	//InetTelnetRead (g_hCmonAgent, szTemp, sizeof(szTemp)-1, &iByte, 100);
    	//strcat(szBuffer, szTemp);
    	iPos = FindPattern (szBuffer, 0, -1, "ret", 0, 0);
        //if (iByte == 0 && iTotalBytesRead > 0 && iPos >= 0)
        //    break;
        //iTotalBytesRead += iByte;
    	
        //dTimeElapsed = Timer() - dStartTime;
        //if( dTimeElapsed > iTimeout ) 
        //{
           //sprintf(szLogMsg,"CMON Agent Timeout...");
           //iResult = -1;
           //goto Error;
        //}	
    //}
    
    EELog_INEventLogLogEvent (g_hEventLog, NULL, szBuffer);
    
    *PassFail = PASS;

    for(iPatternNum = 0;iPatternNum < iTotlePatternNum;iPatternNum++)
    {
        ptrTempPattern = (_FIND_PATTERN_GET_SRING*)(pszFindPattern + iPatternNum);
        iPos = FindPattern(szBuffer ,0,-1,ptrTempPattern->cFindPattern,0,0); 
        if(iPos < 0) 
        { 
           sprintf( szLogMsg,"Cant find the pattern :%s string.",ptrTempPattern->cFindPattern);	
           EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
           *PassFail = 0;
           iResult = -1;
           
           goto Error;
        }
        else
        {
        	ptr = strstr(szBuffer, ptrTempPattern->cFindPattern);
        	if(NULL == ptr)
        	{
        	    return  -1;
            }
            
            //Get the String address
        	ptr = ptr + strlen(ptrTempPattern->cFindPattern) + ptrTempPattern->iOffsetFromPattern;

            iGetStringLength = strlen(ptrTempPattern->cGetString);
            if(0 >= iGetStringLength)
            {
                return  -1;
            }
            
            strncpy(szTempGetString,ptr,iGetStringLength);
            szTempGetString[iGetStringLength + 1] = '\0';
            
            if(0 != strcmp(szTempGetString,ptrTempPattern->cGetString))
            {
                sprintf( szLogMsg,"Cant find the pattern :%s string.",ptrTempPattern->cGetString);	
                EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);            
                *PassFail = 0;
                iResult = -1;

                goto Error;
            }
            iResult = 0;            
        }
    }
 

Error:	

    if(iResult <0)
    {
         EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
        *PassFail = -999;
    }

    return iResult; 
}

#endif

int PostMessageToGui(char *pszMessage)
{
   // Post message to GUI
   if ( g_hThreadPostUIMessage )
   {
      TS_ThreadPostUIMessageEx (g_hThreadPostUIMessage, NULL, 
                                TS_UIMsg_ProgressText, 0.0, pszMessage, 0, VFALSE);                                    
      TS_ThreadPostUIMessageEx (g_hThreadPostUIMessage, NULL, 
                                TS_UIMsg_ProgressText, 0.0, pszMessage, 0, VTRUE);                                    
   }
   
   return 0;   
}

/****************************************************************************
 *---------------------------- End source File -----------------------------*
 ****************************************************************************/
